name = "Maria"
age = 11
print(type(name), type(age))
print("my name is 'name', my age is 'age'.")
<class 'str'> <class 'int'>
my name is 'name', my age is 'age'.
name = "Maria"
age = 11
print(type(name), type(age))
print("my name is 'name', my age is 'age'.")
<class 'str'> <class 'int'>
my name is 'name', my age is 'age'.
There are 4 ways I think.
Using normal addition or String concatenation
print('my name is ' + name + ', my age is ' + str(age) + '.')
Using f-string
print(f'my name is {name}, my age is {age}.')
Using .format()
print("my name is {name}, my age is {age}.".format(name=name, age=age))
Using the %
operator
print("my name is %s, my age is %i." % (name, age))
It's not necessary to add quotes for variable to assign its value.
print("my name is",name,"my age is",age)
Edit: @Wei, yeah I missed to notice it too. You can use the punctuation marks as strings using quotes and concatenate them.
So, it needs to be like this
print("My name is",name,",","My age is",age,".")
Additionally, comma
adds a space by default in concatenation
. Inorder to handle this problem you can use plus
as concat operator. But keep in mind, you have to convert integers to string using str()
when you're using +
in concatenation.
print("My name is "+name+","+"My age is "+str(age)+".")