-2
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'.
Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
Wei
  • 7
  • 2

3 Answers3

6

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))
Was'
  • 496
  • 4
  • 21
  • 1
    Side note: ``Using normal addition`` is commonly referred to as ``String concatenation``. And you missed the ``%`` operator. Related answer: https://stackoverflow.com/a/56735266/4349415 – Mike Scotty Oct 08 '21 at 12:50
1

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)+".")
Python learner
  • 1,159
  • 1
  • 8
  • 20
  • 1
    It compiled "my name is Maria my age is 11", what if I want "my name is Maria, my age is 11." like this with punctuation marks please? – Wei Oct 08 '21 at 13:21
0

This should do the work :

name = "Maria"
age = 11

print('my name is ' + name + ', my age is ' + str(age))
tlentali
  • 3,407
  • 2
  • 14
  • 21
terry5546
  • 111
  • 2
  • 10