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

How to implement formatted output in python

coldplay.xixi
coldplay.xixiOriginal
2020-10-26 10:34:4640840browse

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)

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)

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

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

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!

Statement:
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