Home > Backend Development > Python Tutorial > How to implement formatted output in python

How to implement formatted output in python

coldplay.xixi
Release: 2020-10-26 15:32:16
Original
40850 people have browsed it

Python formatted output method: 1. Use [%] placeholder, the code is [% (name, name, age, job, hobby)]; 2. Use dictionary placeholder, the code is [% dic]; 3. When [%] is used as a string in formatted output, use [%%].

How to implement formatted output in python

Python formatted output method:

Format output one, use % placeholder

name = input("请输入您的姓名")
age = int(input("请输入您的年龄"))
job = input("请输入您的工作")
hobby = input("请输入您的爱好")
msg = '''
====== Info of %s ======
name : %s
age : %d
job : %s
hobby : %s
====== end ======
''' % (name, name, age, job, hobby)
print(msg)
Copy after login

Formatted output two, use dictionary placeholder

dic = {"name":"ghl","age":"24","job":"sre","hobby":"cycling"}
name = input("请输入您的姓名")
age = int(input("请输入您的年龄"))
job = input("请输入您的工作")
hobby = input("请输入您的爱好")
msg = '''
====== Info of %(name)s ======
name : %(name)s
age : %(age)s
job : %(job)s
hobby : %(hobby)s
====== end ======
''' % dic
print(msg)
Copy after login

%When used as a string in formatted output, use %%

msg = '我叫%s,今年%d,学习进度5%%' %("ghl",18)
print(msg)
Copy after login

Related free learning recommendations: python video tutorial

The above is the detailed content of How to implement formatted output in python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template