Coder777
Coder777

Reputation: 23

How to join two variables together to create a username?

How to join the two variables together to create a username?

name = input("enter name: ")
age = int(input("enter age: "))
username = (name[0,3] + str(age))
print(username)

This gives me a type error but I am unsure as to what I am doing wrong. I want the first three letters of the name to be joined with the age to form a new username. Can someone please help me!

Upvotes: 1

Views: 191

Answers (1)

bassman
bassman

Reputation: 195

The expression username = (name[0,3]+str(age)) raises exception because of this part : name[0,3]

You can not use comma while indexing strings. Use name[0:3], or basically name[:3] instead.

And then, your code becomes username = (name[:3] + str(age)).

Note : Also you don't need the outer parenthesis in username = (name[:3] + str(age)).

Upvotes: 5

Related Questions