How do you combine text and numbers in python?

I was trying to concatenate a string and a number in Python. It gave me an error when I tried this:

"abc" + 9

The error is:

Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    "abc" + 9
TypeError: cannot concatenate 'str' and 'int' objects

Why am I not able to do this?

How can I concatenate a string and a number in Python?

How do you combine text and numbers in python?

asked Aug 8, 2011 at 11:35

0

Python is strongly typed. There are no implicit type conversions.

You have to do one of these:

"asd%d" % 9
"asd" + str(9)

answered Aug 8, 2011 at 11:37

Jochen RitzelJochen Ritzel

101k29 gold badges195 silver badges190 bronze badges

2

If it worked the way you expected it to (resulting in "abc9"), what would "9" + 9 deliver? 18 or "99"?

To remove this ambiguity, you are required to make explicit what you want to convert in this case:

"abc" + str(9)

answered Aug 8, 2011 at 11:38

vstrienvstrien

2,5273 gold badges27 silver badges46 bronze badges

0

Since Python is a strongly typed language, concatenating a string and an integer, as you may do in Perl, makes no sense, because there's no defined way to "add" strings and numbers to each other.

Explicit is better than implicit.

...says "The Zen of Python", so you have to concatenate two string objects. You can do this by creating a string from the integer using the built-in str() function:

>>> "abc" + str(9)
'abc9'

Alternatively, use Python's string formatting operations:

>>> 'abc%d' % 9
'abc9'

Perhaps better still, use str.format():

>>> 'abc{0}'.format(9)
'abc9'

The Zen also says:

There should be one-- and preferably only one --obvious way to do it.

Which is why I've given three options.

How do you combine text and numbers in python?

answered Aug 8, 2011 at 11:37

How do you combine text and numbers in python?

johnsywebjohnsyweb

132k23 gold badges178 silver badges239 bronze badges

1

Either something like this:

"abc" + str(9)

or

"abs{0}".format(9)

or

"abs%d" % (9,)

answered Aug 8, 2011 at 11:39

xubuntixxubuntix

2,32518 silver badges19 bronze badges

2

You have to convert the int into a string:

"abc" + str(9)

answered Aug 8, 2011 at 11:37

How do you combine text and numbers in python?

senderlesenderle

139k35 gold badges206 silver badges231 bronze badges

You would have to convert the int into a string.

# This program calculates a workers gross pay

hours = float(raw_input("Enter hours worked: \n"))

rate = float(raw_input("Enter your hourly rate of pay: \n"))

gross = hours * rate

print "Your gross pay for working " +str(hours)+ " at a rate of " + str(rate) + " hourly is $"  + str(gross)

Paul Roub

36k27 gold badges80 silver badges88 bronze badges

answered Dec 6, 2017 at 1:01

Do it like this:

"abc%s" % 9
#or
"abc" + str(9)

How do you combine text and numbers in python?

answered Aug 8, 2011 at 11:37

How do you combine text and numbers in python?

guettliguettli

23.9k66 gold badges308 silver badges581 bronze badges

Can we add string and number in Python?

Python add strings with + operator The easiest way of concatenating strings is to use the + or the += operator. The + operator is used both for adding numbers and strings; in programming we say that the operator is overloaded. Two strings are added using the + operator.

How do you concatenate a string and integer in Python?

Normally, string concatenation is done in Python using the + operator. However, when working with integers, the + represents addition.

How do I combine numbers and strings?

To concatenate a string to an int value, use the concatenation operator. Here is our int. int val = 3; Now, to concatenate a string, you need to declare a string and use the + operator.

How do you concatenate a string and a variable in Python?

Use the + operator.
str1="Hello".
str2="World".
print ("String 1:",str1).
print ("String 2:",str2).
str=str1+str2..
print("Concatenated two different strings:",str).

Tải thêm tài liệu liên quan đến bài viết How do you combine text and numbers in python?