Printing Values from a list without spaces in python 2.7
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?)
Print list without spaces
You need to use something like:
print('[{0}]'.format(','.join(map(str, l))))
python 2.7.5+ print list without spaces after the commas
The data in hand is a list of numbers. So, first we convert them to strings and then we join the join the strings with str.join
function and then print them in the format [{}]
using str.format
, here {}
represents the actual joined string.
data = [1,2, 3, 4]
print(data) # [1, 2, 3, 4]
print("[{}]".format(",".join(map(repr, data)))) # [1,2,3,4]
data = ['aaa','bbb', 'ccc', 'ddd']
print(data) # ['aaa', 'bbb', 'ccc', 'ddd']
print("[{}]".format(",".join(map(repr, data)))) # ['aaa','bbb','ccc','ddd']
If you are using strings
data = ['aaa','bbb', 'ccc', 'ddd']
print("[{}]".format(",".join(map(repr, data))))
Or even simpler, get the string representation of the list with repr
function and then replace all the space characters with empty strings.
print(repr(data).replace(" ", "")) # [1,2,3,4]
Note: The replace method will not work if you are dealing with strings and if the strings have space characters in them.
How to print a list using splat-operator (*) without spaces
You get that result because print
automatically puts spaces between the passed arguments. You need to modify the sep
parameter to prevent the sep
aration spaces from being inserted:
print(*word, sep="") # No implicit separation space
stackoverflow
How to print without spaces in python 3?
You can use the sep
argument to print:
>>> income = 50000
>>> print("Your income tax is $", income, ".", sep='')
Your income tax is $50000.
Or use the str.format
:
>>> print("Your income tax is ${}.".format(income))
Your income tax is $50000.
Print without space in python 3
You can use the sep
parameter to get rid of the spaces:
>>> print("a","b","c")
a b c
>>> print("a","b","c",sep="")
abc
I don't know what you mean by "Java style"; in Python you can't add strings to (say) integers that way, although if a
and b
are strings it'll work. You have several other options, of course:
>>> print("a = ", a, ", b = ", b, sep="")
a = 2, b = 3
>>> print("a = " + str(a) + ", b = " + str(b))
a = 2, b = 3
>>> print("a = {}, b = {}".format(a,b))
a = 2, b = 3
>>> print(f"a = {a}, b = {b}")
a = 2, b = 3
The last one requires Python 3.6 or later. For earlier versions, you can simulate the same effect (although I don't recommend this in general, it comes in handy sometimes and there's no point pretending otherwise):
>>> print("a = {a}, b = {b}".format(**locals()))
a = 2, b = 3
>>> print("b = {b}, a = {a}".format(**locals()))
b = 3, a = 2
Related Topics
Python Selenium - Element Is Not Currently Interactable and May Not Be Manipulated
Checking If a Button Has Been Pressed in Python
Python: Editing List While Iterating Over It
Python - How to Pad the Output of a MySQL Table
Regex to Remove Commas Before a Number in Python
How to Read a Specific Line from a Text File in Python
How to Execute Local Python Scripts in Jenkins Ui
How to Compare 2 Indexes in Same List in Python
How to Append a List Withoud Adding the Quote
Python: How to Print Separate Lines from a List
Removing Backslashes from a String in Python
Spark Equivalent of If Then Else
Passing a List of Values from Python to the in Clause of an SQL Query
How to Start a Background Process in Python
How to Concatenate/Append Multiple Spark Dataframes Column Wise in Pyspark