Python - Any other way to add a space between 2 concatenating strings using print() function
If you really like to make use of the plus sign to concatenate strings with a delimiter. You can use plus to make a list first and than apply something that will delimit the arguments in the list.
# So, let's make the list first
str_lst = [a] + [b] # you could also do [a, b], but we wanted to make use of the plus sign.
# now we can for example pass this to print and unpack it with *. print delimits by space by default.
print(*str_list) # which is the same as print(str_list[0], str_list[1]) or print(a, b), but that would not make use of the plus sign.
# Or you could use join the concetenate the string.
" ".join(*str_list)
Okay, so hope you learned some new things today. But please don't do it like this. This is not how it meant to be done.
How do you add space between characters except for a word?
One way using re.sub
:
s = "abc(word)def(word)oiu"
re.sub("\(.+?\)", lambda x: x.group(0).replace(" ", ""), " ".join(s))
Output:
'a b c (word) d e f (word) o i u'
This will first put spaces in between all characters, and remove spaces that are between brackets.
How do add whitespace in between a word in python
If you want to add more whitespaces, you can just added it inside the string. If you want a new line, you can use "\n" in your string, which indicate the start of a new line.
Check this link to find out more about escape characters:
https://www.tutorialspoint.com/escape-characters-in-python
How do I add space between two variables after a print in Python
A simple way would be:
print str(count) + ' ' + str(conv)
If you need more spaces, simply add them to the string:
print str(count) + ' ' + str(conv)
A fancier way, using the new syntax for string formatting:
print '{0} {1}'.format(count, conv)
Or using the old syntax, limiting the number of decimals to two:
print '%d %.2f' % (count, conv)
Simple adding spaces between variables in python3
There are several ways to get the wanted output:
Concentrating strings
If you want to concentrate your string you use the +
operator.
It will concentrate your strings EXACTLY the way you provide them in your code.
Example:
>>> stringA = 'This is a'
>>> stringB = 'test'
>>> print(stringA + stringB)
'This is atest'
>>> print(stringA + ' ' + stringB)
'This is a test'
Printing on the same line
If you simply want to print multiple strings on the same line you can provide your strings to the print
function as arguments seperated with a ,
Example:
>>> print('I want to say:', stringA, stringB)
I want to say: This is a test
Formatting strings
The most used way is string formatting. This can be done in two ways:
- Using the format
function
- Using the 'old' way with %s
Example:
>>> print('Format {} example {}'.format(stringA, stringB))
Format This is a example test
>>> print('Old: %s example %s of string formatting' % (stringA, stringB))
Old: This is a example test of string formatting
Of course those examples can be combined in any way you want.
Example:
>>> stringC = 'normally'
>>> print((('%s strange {} no one ' % stringA) + stringC).format(stringB), 'uses')
This is a strange test no one normally uses
In Python how do I make a space between a list and string?
Just do this:
catNames = []
while True:
print('Enter the name of cat ' + str(len(catNames) + 1) +
' (Or enter nothing to stop.):')
name = input()
if name == '':
break
catNames = catNames + [name] # list concatenation
print('The cat names are:')
#for name in range(len(catNames)):
#print(name)
for i in range(len(catNames)):
print(catNames[i] + " " + str(i))
Though its always better to use f-string
s or format
:
catNames = []
while True:
print('Enter the name of cat ' + str(len(catNames) + 1) +
' (Or enter nothing to stop.):')
name = input()
if name == '':
break
catNames = catNames + [name] # list concatenation
print('The cat names are:')
#for name in range(len(catNames)):
#print(name)
for i in range(len(catNames)):
print(f"{catNames[i]} {str(i)}")
Using f-string
s makes the code much cleaner, and simpler to understand. Look here for more details. Note that f-strings are only for python 3.6 or above. If you have a version of python below 3.6, check my previous answer to see how you can use f-strings in below python 3.6.
Related Topics
Get Discord User Id from Username
Combine Date and Time Columns Using Python Pandas
Typeerror: the Json Object Must Be Str, Not 'Bytes'
How to Change Python Version in Command Prompt If I Have 2 Python Version Installed
Replace Single Quote With Double Quote in a String Python
Python Db-Api: Fetchone VS Fetchmany VS Fetchall
Check If Value from One Dataframe Exists in Another Dataframe
How to Divide Each Column of Pandas Dataframe by a Series
Key Error When Selecting Columns in Pandas Dataframe After Read_Csv
Find and Replace Specific Values Within 2D Array
Python Format Size Application (Converting B to Kb, Mb, Gb, Tb)
How to Verify If a Button Is Enabled and Disabled in Webdriver Python