Create the desired output with format

Asked 1 years ago, Updated 1 years ago, 294 views

name = 'Anyone'
snum = '22808080'
addr = 'Nam-gu, Ulsan Metropolitan City'
mp = '0101234567'

Let's make a format statement so that it can be output as follows.

 1. Name: anyone
    2. Academic number: 22808080
    3. Address: Nam-gu, Ulsan Metropolitan City
    4. Phone number: 0101234567
for w in words:
     print(name)
     print(snum)
     print(addr)
     print(mp)

I can't do it like this.

I don't know how to organize it in format, so I'd appreciate it if you could tell me.

python

2022-10-18 08:54

1 Answers

The format is as follows

Write down the format form you want in quotation marks, and the variable parts to be filled in order are {0}, {1}..Empty it like this.

Use .format followed by {0}, {1} to write down the variables you want to actually put in.

1. Name: If you want to print out '1.Name:' is a format form print(1.Name: {0}'.You can call format(name)).

Similarly

print('2. School number: {0}'.format(snum))
print('3. Address: {0}'.format(addr))
print('4. Phone number: {0}'.format(mp))

You can do it with this.


2022-10-18 08:54

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.