How to append an integer to a string?

+1 vote

I have an integer and I have a string. Now I want to create a new string which is got by concatenated the string and the integer. But I don’t understand how to do this?

I am using this following code:

a = 10

new = “hello” + a

But I get this error:

File "program.py", line 2,

new = “hello” + a

TypeError: Can't convert 'int' object to str implicitly
Aug 16, 2018 in Python by ana1504.k
• 7,870 points
49 views

1 answer to this question.

0 votes

You can not directly concatenate two different data-types. You have to type-cast it first.

Try this code:

name = 'b' + str(num)

or

name = 'b%s' % num
answered Aug 16, 2018 by Omkar
• 67,290 points

Related Questions In Python

0 votes
1 answer

How to convert an integer to a string using Python?

Here is an easy solution: def numberToBase(n, b): ...READ MORE

answered Nov 23, 2018 in Python by Nymeria
• 3,520 points

edited Dec 12, 2018 by Nymeria 40 views
0 votes
1 answer

How to print first character of each word in upper case of a string in Python

You can use split() using space as ...READ MORE

answered Jun 1, 2018 in Python by george
• 200 points
1,581 views
0 votes
1 answer

How to get the size of a string in Python?

If you are talking about the length ...READ MORE

answered Jun 4, 2018 in Python by ariaholic
• 7,340 points
74 views
0 votes
1 answer

How do you append to a file?

with open("test.txt", "a") as myfile: myfile.write("appended text" ...READ MORE

answered Jul 27, 2018 in Python by Priyaj
• 56,520 points
24 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 175 views
+4 votes
6 answers
+3 votes
2 answers

how to print array integer without [] bracket in python like result = 1,2,3,4,5

Hey @abhijmr.143, you can print array integers ...READ MORE

answered Aug 4, 2018 in Python by Omkar
• 67,290 points

edited Aug 8, 2018 by Omkar 502 views
+1 vote
2 answers

How to convert list to string

mylist = [1, 2, 3] ‘’.join(map(str, mylist)) ==> ...READ MORE

answered Aug 21, 2018 in Python by Omkar
• 67,290 points
37 views