M. Ibtihaz Islam
M. Ibtihaz Islam

Reputation: 69

Solving TypeError when concatenating a string

I assigned these variables:

x = "1"
name = str(input("Enter Name  :"))
gender = str(input("Enter Gender (M/F)  :")).lower
year = int(input("Enter Year of Birth  :"))   
postcode = str(input("Enter Postcode  :"))

Then I got I got a part of the string postcode.

partCode = postcode[0:3]

Then I converted the year in to a string.

birthYear = str(year)

After, I tried concatenating all the strings:

username = name + birthYear + gender + partCode + x

And I receive this error:

TypeError: can only concatenate str (not "builtin_function_or_method") to str

How can I solve it?

Upvotes: 1

Views: 200

Answers (3)

Mayank
Mayank

Reputation: 158

enter image description here

The above error is because gender is not a string

replace

gender = str(input("Enter Gender (M/F)  :")).lower

with

gender = str(input("Enter Gender (M/F)  :")).lower()

Upvotes: 3

Kenan
Kenan

Reputation: 14094

You don't need to convert any of the input variables their all read in as str.

This will do

x = "1"
name = str(input("Enter Name  :"))
gender = str(input("Enter Gender (M/F)  :")).lower()
year = str(input("Enter Year of Birth  :"))   
postcode = str(input("Enter Postcode  :"))

The issue is you didn't have () for the lower function so your gender variable was <built-in method lower of str object at 0x7fb7152ffc38>. That should explain your error

Upvotes: 0

AmirHmZ
AmirHmZ

Reputation: 556

Try this :

x = "1"
name = str(input("Enter Name  :"))
gender = str(input("Enter Gender (M/F)  :")).lower()
year = int(input("Enter Year of Birth  :"))   
postcode = str(input("Enter Postcode  :"))

partCode = postcode[0:3]
birthYear = str(year)
username = name + birthYear + gender + partCode + x

Upvotes: 1

Related Questions