Python Forum
can only concatenate str (not "int") to str
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
can only concatenate str (not "int") to str
#2
Please use proper code tags while posting a thread.
The problem is python can't just join a string and an integer together, because they are two different things and hence Python has no clue - should it add them like an integer or join them like a string ?? Confused
So there are 2 ways to defeat this -
  1. First, you can change the integer into a string, like
    print('hello ' + name + ' you are ' + str(age) + " years old")
    OR

  2. You can use commas -
    print('hello ' , name , ' you are ' , age , " years old")
Python doesn't know how to add a word and a number, so it says "cannot concatenate 'str' and 'int' objects." A word that you put in quotes is just a string of letters called a str in python. Numbers that don't have a decimal point are integers and are called int in python.
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply


Messages In This Thread
RE: can only concatenate str (not "int") to str - by pyzyx3qwerty - May-27-2020, 10:56 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Concatenate str JohnnyCoffee 2 2,975 May-01-2021, 03:58 PM
Last Post: JohnnyCoffee
  Concatenate two dataframes moralear27 2 1,933 Sep-15-2020, 08:04 AM
Last Post: moralear27
  Concatenate two dictionaries harish 3 2,444 Oct-12-2019, 04:52 PM
Last Post: strngr12

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020