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 :
JavaScript
x
8
1
name = 'John Smith'
2
age = 25
3
location = 'CHICAGO' # change this to lower case when printing
4
print('My name is %s.')
5
print('My name is %s, and I live in %s' )
6
print(f'My name is {name}.')
7
'and I live in location.lower()
8
How can I get the results from the top?
Advertisement
Answer
JavaScript
1
7
1
#!/usr/bin/env python3
2
3
name = 'john smith'
4
age = 25
5
location = 'chicago'
6
print ('My name is %s, and I live in %s. I am %s years old.' % (name, location, str(age)))
7
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/