Printing variables and strings in Python

Tags: , ,



I am trying to print the following lines :

‘My name is John Smith.’

‘My name is John Smith, and I live in CHICAGO’

and I live in chicago’

My code below :

 name = 'John Smith'
 age = 25
 location = 'CHICAGO' # change this to lower case when printing
 print('My name is %s.')
  print('My name is %s, and I live in %s' )
  print(f'My name is {name}.')
  'and I live in location.lower()

How can I get the results from the top?

Answer

#!/usr/bin/env python3

name = 'john smith'
age = 25
location = 'chicago'
print ('My name is %s, and I live in %s. I am %s years old.' %  (name, location, str(age)))

Output:

My name is john smith, and I live in chicago. I am 25 years old.

By the way i recommend you to read this article: https://realpython.com/python-string-formatting/



Source: stackoverflow