You can use join with list comprehension:
>>> l=range(5)
>>> print l
[0, 1, 2, 3, 4]
>>> ''.join(str(i) for i in l)
'01234'
Also, don't use list as a variable name since it is a built-in function.
You can use join with list comprehension:
>>> l=range(5)
>>> print l
[0, 1, 2, 3, 4]
>>> ''.join(str(i) for i in l)
'01234'
Also, don't use list as a variable name since it is a built-in function.
In Python 3 you can do:
print(*ll, sep='')
Where ll is a list of ints. Or for the OP's specific use-case:
print(*range(1,int(input())+1), sep='')
Your output will be like this if input = 4 :
1234
python - Print list without spaces - Stack Overflow
How to convert list to string without brackets or spaces?
join method of strings.
','.join(test)
Although, since you've got integers in that list, you'll have to convert them to strings.
','.join(str(num) for num in test)More on reddit.com
Printing Values from a list without spaces in python 2.7 - Stack Overflow
Printing list as string without brackets or commas
This seems like it has a simple solution but I can't seem to find it. I have a list
test = [1, 2, 3, 4]
and I want to convert it to the string and remove the brackets and spaces
1,2,3,4
I've found that I can print the list correctly by using
print(*test, sep=',')
but I want to concatenate the string for use elsewhere, not just print it.
Any ideas?
From your comments on @jftuga answer, I guess that the input you provided is not the one you're testing with. You have mixed contents in your list.
My answer will fix it for you:
lst = ['A','B',1,2]
print("".join([str(x) for x in lst]))
or
print("".join(map(str,lst)))
I'm not just joining the items since not all of them are strings, but I'm converting them to strings first, all in a nice generator comprehension which causes no memory overhead.
Works for lists with only strings in them too of course (there's no overhead to convert to str if already a str, even if I believed otherwise on my first version of that answer: Should I avoid converting to a string if a value is already a string?)
Try this:
a = "".join(list1)
print(a)
This will give you: AB12
Also, since list is a built-in Python class, do not use it as a variable name.
I'm working on a homework assignment for my online class and got it finished down to this:
I'm having an issue finding out how to get the string to fulfill the necessary tasks and still print in the way desired below.
Write code that does the following:
-
Compute the number of words in variable sentence that contain either an "p" or an "g". Store the result in the variable numPorG.
-
Creates a string strPorG composed of those words in sentence that contain an "p" or "g". Each word in strPorG must be separated by a comma.
Print the variables numPorG and strPorG as shown below.
Do not double-count words that contain both an p and an g. Use the in operator to test for existence.
sentence = "python is a high level general purpose programming language that can be applied to many different classes of problems"
##### My code below:
sentence = sentence.split()
numPorG = 0
strPorG = []
for word in sentence:
if 'p' in word or 'g' in word:
numPorG += 1
strPorG.append(str(word))
strPorG = str(strPorG) # I think this part is redundant
print("Number of words containing p or g is", numPorG)
print("Those words are", strPorG)
#####
Output I'm getting:
Number of words containing p or g is 8
Those words are ['python', 'high', 'general', 'purpose', 'programming', 'language', 'applied', 'problems']
Expected output:
Number of words containing p or g is 8
Those words are python,high,general,purpose,programming,language,applied,problems
You can apply the list as separate arguments:
print(*L)
and let print() take care of converting each element to a string. You can, as always, control the separator by setting the sep keyword argument:
>>> L = [1, 2, 3, 4, 5]
>>> print(*L)
1 2 3 4 5
>>> print(*L, sep=', ')
1, 2, 3, 4, 5
>>> print(*L, sep=' -> ')
1 -> 2 -> 3 -> 4 -> 5
Unless you need the joined string for something else, this is the easiest method. Otherwise, use str.join():
joined_string = ' '.join([str(v) for v in L])
print(joined_string)
# do other things with joined_string
Note that this requires manual conversion to strings for any non-string values in L!
Although the accepted answer is absolutely clear, I just wanted to check efficiency in terms of time.
The best way is to print joined string of numbers converted to strings.
print(" ".join(list(map(str,l))))
Note that I used map instead of loop. I wrote a little code of all 4 different ways to compare time:
import time as t
a, b = 10, 210000
l = list(range(a, b))
tic = t.time()
for i in l:
print(i, end=" ")
print()
tac = t.time()
t1 = (tac - tic) * 1000
print(*l)
toe = t.time()
t2 = (toe - tac) * 1000
print(" ".join([str(i) for i in l]))
joe = t.time()
t3 = (joe - toe) * 1000
print(" ".join(list(map(str, l))))
toy = t.time()
t4 = (toy - joe) * 1000
print("Time",t1,t2,t3,t4)
Result:
Time 74344.76 71790.83 196.99 153.99
The output was quite surprising to me. Huge difference of time in cases of 'loop method' and 'joined-string method'.
Conclusion: Do not use loops for printing list if size is too large( in order of 10**5 or more).