My code looks like this:
name = Joe
print "Hello", name, "!"
My output looks like:
Hello Joe !
How do I remove the space between Joe
and !
?
QuantumFool
6093 gold badges10 silver badges29 bronze badges
asked Apr 20, 2016 at 2:55
TrentWoodburyTrentWoodbury
7692 gold badges11 silver badges22 bronze badges
1
There are several ways of constructing strings in python. My favorite used to be the format function:
print "Hello {}!".format[name]
You can also concatenate strings using the + operator.
print "Hello " + name + "!"
More information about the format function [and strings in general] can be found here: //docs.python.org/2/library/string.html#string.Formatter.format
6 Years Later...
You can now use something called f-Strings
if you're using python 3.6 or newer. Just prefix the string with the letter f
then insert variable names inside some brackets.
print[f"Hello {name}"]
answered Apr 20, 2016 at 3:02
a comma after print will add a blank space.
What you need to do is concatenate the string you want to print; this can be done like this:
name = 'Joe'
print 'Hello ' + name + '!'
Joe
must be put between quotes
to define it as a string.
answered Apr 20, 2016 at 2:57
Reblochon MasqueReblochon Masque
33.9k10 gold badges50 silver badges73 bronze badges
1
>>> print [name]
Joe
>>> print['Hello ', name, '!']
Hello Joe !
>>> print['Hello ', name, '!', sep='']
Hello Joe!
answered Jul 2, 2018 at 9:25
HossHoss
7575 silver badges3 bronze badges
1
You can also use printf
style formatting:
>>> name = 'Joe'
>>> print 'Hello %s !' % name
Hello Joe !
answered Apr 20, 2016 at 5:01
heemaylheemayl
36.6k7 gold badges63 silver badges70 bronze badges
One other solution would be to use the following:
Print 'Hello {} !'.format[name.trim[]]
This removes all the leading and trailing spaces and special character.
answered Apr 20, 2016 at 5:13
AvneeshAvneesh
3053 silver badges12 bronze badges
Remove space between variable and string in Python #
Use a formatted string literal to remove the space between a variable and a string, e.g. result = f'{variable} world'
. The variable can be interpolated right next to the string when using a formatted string literal.
Copied!
variable = 'hello' # ✅ using formatted string literal result = f'{variable} world' print[result] # 👉️ 'hello world' # ----------------------------------------- # ✅ using the addition [+] operator result = variable + ' world' print[result] # 👉️ hello world # ----------------------------------------- # ✅ using str.join[] and str.split[] variable = 'hello ' result = ' '.join[f'{variable} world'.split[]] print[result] # 👉️ 'hello world' # ----------------------------------------- # ✅ remove unnecessary whitespace when printing variable = 'hello ' # 👇️ hello world! print[variable, 'world', '!', sep='']
The first example uses a formatted string literal to remove the space between a variable and a string.
Copied!
variable = 'hello' # ✅ using formatted string literal result = f'{variable} world' print[result] # 👉️ 'hello world' result = f'{variable}world' print[result] # 👉️ 'helloworld'
Formatted string literals [f-strings] let us include expressions inside of a string by prefixing the string with f
.
Copied!
variable = 'hello' variable_2 = 'world' result = f'{variable} {variable_2}!' print[result] # 👉️ 'hello world!'
Make sure to wrap expressions in curly braces - {expression}
.
Alternatively, you can use the addition [+] operator to concatenate the variable and the string without extra whitespace.
Copied!
variable = 'hello' result = variable + ' world' print[result] # 👉️ 'hello world'
If you use the addition operator, you have to make sure that the values on the left and right-hand sides are of type string.
You can use the str[]
class if you need to convert a value to a string, e.g. str[my_number]
.
If you have a string that contains multiple spaces next to one another, you can use the str.split[]
and str.join[]
methods to remove the extra whitespace.
Copied!
variable = 'hello ' result = ' '.join[f'{variable} world'.split[]] print[result] # 👉️ 'hello world' result = ''.join[f'{variable}world'.split[]] print[result] # 👉️ 'helloworld'
The str.split[] method splits the string into a list of substrings using a delimiter.
If no delimiter is passed to the method, the method splits the string on one or more occurrences of whitespace characters.
Copied!
variable = 'hello ' # 👇️ ['hello', 'world'] print[f'{variable} world'.split[]]
The last step is to use the str.join[]
method to join the list of strings.
The str.join method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.
The string the method is called on is used as the separator between the elements.
Copied!
variable = 'hello ' result = ' '.join[f'{variable} world'.split[]] print[result] # 👉️ 'hello world' result = ''.join[f'{variable}world'.split[]] print[result] # 👉️ 'helloworld'
If you need to remove extra spaces when using the print[]
function, set the sep
keyword argument to an empty string.
Copied!
variable = 'hello ' # 👇️ hello world! print[variable, 'world', '!', sep='']
The sep
keyword argument is the separator between the values.
By default, the sep
argument is set to a space.
By setting the argument to an empty string, no extra whitespace is added between the values.