3

Here is my code below if i print it directly i'm not getting any brackets or commas whereas if i store it in a variable and print the variable its getting printed in brackets.

If anyone know to print it without brackets please let me know

Code:

user_name = input('Your Name')
user_year =  int(input('Your Birth Year'))
user_age = 2021 - user_year
user_info = "Age of",user_name,"is",user_age

print(user_info)
print('Age of',user_name,'is',user_age)

Output:

Your Name Sri Govind
Your Birth Year 2005
('Age of', ' Sri Govind', 'is', 16)
Age of  Sri Govind is 16

Process finished with exit code 0
Sri Govind
  • 65
  • 5

3 Answers3

2

Inputting the value of the variables in the string is straightforward.

String Formatting

user_info = "Age of {name} is {age}".format(name=user_name, age=user_age)

f Strings

user_info = f"Age of {user_name} is {user_age}"
NGilbert
  • 485
  • 6
  • 12
1

user_info = "Age of",user_name,"is",user_age this will actually create a tuple. try print(type(user_info)).
you can do user_info = "Age of "+user_name+" is "+str(user_age). This will make it string.

sittsering
  • 1,219
  • 2
  • 6
  • 13
1

for these lines :

user_info = "Age of",user_name,"is",user_age

print(user_info)

for get all value you can use * like below:

print(*user_info)

you can also use f-strings like below:

user_info = f"Age of {user_name} is {user_age}"

print(user_info)
I'mahdi
  • 23,382
  • 5
  • 22
  • 30