If a define a variable x=8 and need an output like "The number is 8" [in the same line], how could I print the string "The number is" and the variable "x" together? Or the second after the first but in the same line?
x = 8 print["The number is", x] OR x = 8 print["The number is " + str[x]]
If you're using Python 3.6 you can make use of f strings. Which is pretty neat: name = 'Don' print[f'Hey my name is {name}.'}]
to sum it up: x = 55 print ["i am" , x] ==> ['i am', 55] print "i am" , x " ==> i am 55 print ["i am " + str[x]] ==> i am 55 print ["i am %s " % x] ==> i am 55 print ["i am {0}".format[str[x]]] ==> i am 55
To print, the data type must be the same. E.g. x = 5 1] print [int [x] + 4] >>> 9 ---------------------Valid 2] print [x +4] >>> 9----------------------------Valid 3] print [x + "what" >>> error----------------- Invalid 4] print [x + str["what"]] >>> error-----------Invalid 5] print [str [x] + " what"] >>> 5 what-------Valid
you can also do x = 8 print["The number is %s" % x] #or print["The number is {0}".format[str[x]]]
In Python say you have
s = "string"
i = 0
print s + i
will give you error, so you write
print s + str[i]
to not get error.
I think this is quite a clumsy way to handle int and string concatenation.
Even Java does not need explicit casting to String to do this sort of concatenation. Is there a better way to do this sort of concatenation, i.e, without explicit casting in Python?
asked Jul 19, 2012 at 10:41
specialscopespecialscope
4,1083 gold badges23 silver badges22 bronze badges
4
Modern string formatting:
"{} and {}".format["string", 1]
answered Jul 19, 2012 at 10:43
2
No string formatting:
>> print 'Foo',0
Foo 0
answered Jul 19, 2012 at 10:44
Burhan KhalidBurhan Khalid
164k18 gold badges238 silver badges276 bronze badges
2
String formatting, using the new-style .format[]
method [with the defaults .format[] provides]:
'{}{}'.format[s, i]
Or the older, but "still sticking around", %
-formatting:
'%s%d' %[s, i]
In both examples above there's no space between the two items concatenated. If space is needed, it can simply be added in the format strings.
These provide a lot of control and flexibility about how to concatenate items, the space between them etc. For details about format specifications see this.
answered Jul 19, 2012 at 10:42
LevonLevon
132k33 gold badges197 silver badges186 bronze badges
Python is an interesting language in that while there is usually one [or two] "obvious" ways to accomplish any given task, flexibility still exists.
s = "string"
i = 0
print [s + repr[i]]
The above code snippet is written in Python 3 syntax, but the parentheses after print were always allowed [optional] until version 3 made them mandatory.
answered Jul 10, 2013 at 7:49
CaitlinGCaitlinG
1,9052 gold badges23 silver badges34 bronze badges
In Python 3.6 and newer, you can format it just like this:
new_string = f'{s} {i}'
print[new_string]
Or just:
print[f'{s} {i}']
answered Jun 29, 2020 at 18:00
0
The format[] method can be used to concatenate a string and an integer:
print[s + "{}".format[i]]
answered Jan 10, 2019 at 18:36
Let's assume you want to concatenate a string and an integer in a situation like this:
for i in range[1, 11]:
string = "string" + i
And you are getting a type or concatenation error.
The best way to go about it is to do something like this:
for i in range[1, 11]:
print["string", i]
This will give you concatenated results, like string 1, string 2, string 3, etc.
answered Oct 23, 2020 at 15:11
You can use the an f-string too!
s = "string"
i = 95
print[f"{s}{i}"]
answered Aug 20, 2021 at 12:41
If you only want to print, you can do this:
print[s, i]
answered Apr 27, 2020 at 12:00