This question already has answers here:
Print a list of space-separated elements
(4 answers)
Closed 2 years ago.
Hi: I am new to python and programing.
I have a silly question that I couldn't solve.
a=[1,2,3,4,5]
for i in a:
print(i,end=' ')
will get a out put:
1 2 3 4 5
There are space between each numbers in the system out print: ( s means space)
1s2s3s4s5s
How can I remove the last space? The correct out put will be :
1s2s3s4s5
a=[1,2,3,4,5]
print(' '.join([str(x) for x in a])
This will first convert each element of 'a' to string and then join all element using join(). It must be noted that ' ' can be replaced with any other symbol as well
There are various ways, but a simple way is join:
print(' '.join(a), end='')
You can also directly use print:
print(*a, sep=' ', end='')
Related
This question already has answers here:
Convert string to variable name in python [duplicate]
(3 answers)
Closed 4 years ago.
I want to print variables vector1 and vector2 in Python 3, without having to write the print code manually. How can I do this? Below you can see the code that I tried using for this.
vectorInput = input("Enter vectors values separated by ',' and vectors separated by ' ': ")
vector1,vector2 = vectorInput.split(" ")
for num in range(1,3):
print({}.format('vector'+num))
Thank you.
Well, you can use comprehensions directly.
[print(i) for i in vectorInput.split(" ")]
Or use list of vectors, as it more fits in your usage pattern, and ou can reuse it later.
vectors = vectorInput.split(" ")
[print(i) for i in vectors]
Or with for
vectors = vectorInput.split(" ")
for i in vectors:
print(i)
This is the shorter version give a try.
for i in input("Enter vectors values separated by ',' and vectors separated by ' ': ").split():
print(f'vector {i}')
If you want i as an integer then replace i with int(i)
This question already has answers here:
Reverse the ordering of words in a string
(48 answers)
Closed 4 years ago.
If my variable is:
string = "first second"
How can I change that to print:
second first
annoyingly simple but I can't find a solution for it!
I'd split the string on ' ' and then join it back:
s = 'first second'
tmp = s.split(' ')
result = ' '.join((tmp[1], tmp[0]))
This question already has answers here:
How do I split a string into a list of words?
(9 answers)
Closed 6 years ago.
I have a string like this:
st = "The most basic data structure in Python is the sequence * Each element of a sequence is assigned a number * its position or index * The first index is zero * the second index is one * and so forth *"
and I want to split into the list like this:
ls =["The most basic data structure in Python is the sequence","Each element of a sequence is assigned a number","its position or index",.....]
I'm just started python, please help me
You can split a string by a character:
yourString = "Hello * my name * is * Alex"
yourStringSplitted = yourString.split('*')
you can use the function str.split to split a string into a array on a specific character :
>>> str.split(st,"*")
['The most basic data structure in Python is the sequence ',
' Each element of a sequence is assigned a number ',
' its position or index ',
' The first index is zero ',
' the second index is one ',
' and so forth ',
'']
This question already has answers here:
Pythonic way to print list items
(13 answers)
Closed 7 years ago.
How do I remove the quote marks and commas and brackets from this result:
encrypt = input("enter your string: ")
encrypt = encrypt.replace(" ","")
encrypt_list = [encrypt[i:i+5] for i in range(0, len(encrypt), 5)]
print (encrypt)
print (encrypt_list)
If the input was: 5 blocks of text test
The output is: ['5bloc', 'ksoft', 'extte', 'st']
I need it to be: 5bloc ksoft extte st
You can use str.join, like this:
>>> s = ' '.join(['5bloc', 'ksoft', 'extte', 'st'])
>>> print(s)
5bloc ksoft extte st
This question already has answers here:
How can I print multiple things on the same line, one at a time?
(18 answers)
Closed last month.
Is it possible to print text on the same line in Python?
For instance, instead of having
1
2
3
I would have
1 2 3
Thanks in advance!
Assuming this is Python 2...
print '1',
print '2',
print '3'
Will output...
1 2 3
The comma (,) tells Python to not print a new line.
Otherwise, if this is Python 3, use the end argument in the print function.
for i in (1, 2, 3):
print(str(i), end=' ') # change end from '\n' (newline) to a space.
Will output...
1 2 3
Removing the newline character from print function.
Because the print function automatically adds a newline character to the string that is passed, you would need to remove it by using the "end=" at the end of a string.
SAMPLE BELOW:
print('Hello')
print('World')
Result below.
Hello
World
SAMPLE Of "end=" being used to remove the "newline" character.
print('Hello', end= ' ')
print('World')
Result below.
Hello World
By adding the ' ' two single quotation marks you create the space between the two words, Hello and World, (Hello' 'World') - I believe this is called a "blank string".
when putting a separator (comma) your problem is easily fixed. But obviously I'm over four years too late.
print(1, 2, 3)