Home> Common Problem> body text

How to write python output statement

百草
Release: 2023-06-20 13:46:39
Original
9000 people have browsed it

Python uses the "print()" function when outputting statements. Its syntax is: "print(*object, sep='', end='\n', file=sys.stdout)", "object " is the output object. If you need to output multiple objects, they need to be separated by commas. "sep" is used to separate multiple objects. "end" is the end of the output. The default is "\n". "file" is to be written. file object.

How to write python output statement

The operating system of this tutorial: Windows 10 system, Python version 3.11.2, DELL G3 computer.

When you want to output content in python, you can use the output statement print. We have already mastered the basic output format. In fact, the print() function can output multiple variables at the same time, and it has more rich functions.

1. Overview

print(*object, sep='',end='\n',file=sys.stdout)

object——the output object , if you want to output multiple objects, you need to separate them (comma)

print('abcd','efg') #结果:abcdefg
Copy after login

sep - used to separate multiple objects

print('abcd','efg',sep=":") #结果:abcd:efg
Copy after login

end - the end of the output, the default is\n

print('abcd',end='') print('efg',end='') #结果:abcdefg 输出不换行
Copy after login

file——The file object to be written

2. Variable output

print can output any type of data

As shown in the figure

age=17 print(age) #结果:17 name='LIKE' print(name) #结果:LIKE list=[17,'LIKE'] print(list) #结果:[17, 'LIKE'] tuple=(17,'LIKE') print(tuple) #结果:(17, 'LIKE') dict={'age':17,'name':'LIKE'} print(dict) #结果:{'age': 17, 'name': 'LIKE'}
Copy after login

3. Formatted output

There are three formatted outputs of print

1. %

This method comes from the c language, the specific operation is as follows

print('%d'%age) #十进制 17 print('%o'%age) #八进制 21 print('%x'%age) #十六进制 11 print('%.2f'%age) #保留两位小数 17.00 print('%.2e'%age) #保留两位小数(用科学计数法) 1.70e+01 print('%.2g'%age) #保留两位小数(在保证六位有效数字的前提下,使用小数方式,长度超过六位用科学计数法)17 print('%s'%age) #输出字符串 17 char='P' print('%c'%char) #输出单个字符 P
Copy after login

There are others, which are usually difficult to use, so I won’t go into details.

2.str.format()

The basic usage of format is to use {} and: instead of %, but the format function is more powerful than %. Compared with %, it has no limit on the number of parameters. The positions can also be out of order. The main functions of format are as follows:

(1) Indexing, filling and interception

print("{},{}".format(age,name)) #按默认顺序 输出 17,LIKE print("{1},{0}".format(age,name)) #按索引顺序 输出 LIKE,17 print("{gender}".format(gender='boy')) #按参数名匹配(参数未确定) 输出boy print("{0} {2} {1}".format('a','b','c')) #通过位置填充 输出 a c b print("年龄:{0[0]} 名字:{0[1]}".format(list)) #按列表索引 输出 年龄:17 名字:LIKE print("年龄:{0[0]} 名字:{0[1]}".format(tuple)) #按元组索引 输出 年龄:17 名字:LIKE print("年龄:{age} 名字:{name}".format(**dict)) #按字典设置 输出 年龄:17 名字:LIKE print("{:.2}".format(list[1])) #截取字符串前5个字符 输出LI #索引和参数可以混搭(但是命名参数得写到最后),索引和默认不行
Copy after login

(2) Type conversion

print("我的名字是{!s}".format("LIKE")) #相当于str() 输出我的名字是LIKE print("我{!r}岁".format("17")) #相当于repr() 输出我'17'岁
Copy after login

(3) Format Number

print('{:b}'.format(age)) #输出二进制 10001 print('{:d}'.format(age)) #输出十进制 17 print('{:o}'.format(age)) #输出八进制 21 print('{:x}'.format(age)) #输出十六 进制 11 print("{:.2f}".format(17.0000)) #保留两位小数 输出17.00 print("{:+},{:+},{: },{: }".format(-17,17,-17,17)) #在正数前加+ 在正数前加空格 输出 -17,+17,-17, 17 print("{:,}".format(5201314)) #用逗号做千位分隔符 输出 5,201,314 print("{:.2%}".format(0.9999)) #表示一个小数点后留两位的百分比 输出 99.99%
Copy after login

(4) Alignment

print("{:*>5}".format("LIKE")) #右对齐 宽度为5,不足用‘>’前的字符(只能为字符)补齐,默认为空格 输出 *LIKE print("{:*<5}".format("LIKE")) #左对齐 输出LIKE* print("{:*^10}".format("LIKE")) #居中 输出***LIKE***
Copy after login

3.f"{}"

Mainly call existing elements

def function(): return "LIKE" print(f"年龄:{age},姓名:{name}") #调用变量 输出 年龄:17,姓名:LIKE print(f"年龄:{list[0]},姓名:{list[1]}") #调用列表元素 输出 年龄:17,姓名:LIKE print(f"年龄:{tuple[0]},姓名:{tuple[1]}") #调用元组元素 输出 年龄:17,姓名:LIKE print(f"年龄:{dict['age']},姓名:{dict['name']}") #调用字典元素 输出 年龄:17,姓名:LIKE print(f"{1+1}") #调用并计算表达式 输出 2 print(f"姓名:{function()}") #调用并计算函数 输出 姓名:LIKE
Copy after login

4. Attachment Picture

from os import sep #print(*object, sep='',end='\n',file=sys.stdout) print('abcd','efg') #结果:abcdefg print('abcd','efg',sep=":") #结果:abcd:efg print('abcd',end='') print('efg',end='') #结果:abcdefg 输出不换行 age=17 print(age) #结果:17 name='LIKE' print(name) #结果:LIKE list=[17,'LIKE'] print(list) #结果:[17, 'LIKE'] tuple=(17,'LIKE') print(tuple) #结果:(17, 'LIKE') dict={'age':17,'name':'LIKE'} print(dict) #结果:{'age': 17, 'name': 'LIKE'} print('%d'%age) #十进制 17 print('%o'%age) #八进制 21 print('%x'%age) #十六进制 11 print('%.2f'%age) #保留两位小数 17.00 print('%.2e'%age) #保留两位小数(用科学计数法) 1.70e+01 print('%.2g'%age) #保留两位小数(在保证六位有效数字的前提下,使用小数方式,长度超过六位用科学计数法)17 print('%s'%age) #输出字符串 17 char='P' print('%c'%char) #输出单个字符 P print("{},{}".format(age,name)) #按默认顺序 输出 17,LIKE print("{1},{0}".format(age,name)) #按索引顺序 输出 LIKE,17 print("{gender}".format(gender='boy')) #按参数名匹配(参数未确定) 输出boy print("{0} {2} {1}".format('a','b','c')) #通过位置填充 输出 a c b print("年龄:{0[0]} 名字:{0[1]}".format(list)) #按列表索引 输出 年龄:17 名字:LIKE print("年龄:{0[0]} 名字:{0[1]}".format(tuple)) #按元组索引 输出 年龄:17 名字:LIKE print("年龄:{age} 名字:{name}".format(**dict)) #按字典设置 输出 年龄:17 名字:LIKE print("{:.2}".format(list[1])) #截取字符串前5个字符 输出LI #索引和参数可以混搭(但是命名参数得写到最后),索引和默认不行 print("我的名字是{!s}".format("LIKE")) #相当于str() 输出我的名字是LIKE print("我{!r}岁".format("17")) #相当于repr() 输出我'17'岁 print('{:b}'.format(age)) #输出二进制 10001 print('{:d}'.format(age)) #输出十进制 17 print('{:o}'.format(age)) #输出八进制 21 print('{:x}'.format(age)) #输出十六 进制 11 print("{:.2f}".format(17.0000)) #保留两位小数 输出17.00 print("{:+},{:+},{: },{: }".format(-17,17,-17,17)) #在正数前加+ 在正数前加空格 输出 -17,+17,-17, 17 print("{:,}".format(5201314)) #用逗号做千位分隔符 输出 5,201,314 print("{:.2%}".format(0.9999)) #表示一个小数点后留两位的百分比 输出 99.99% print("{:*>5}".format("LIKE")) #右对齐 宽度为5,不足用‘>’前的字符(只能为字符)补齐,默认为空格 输出 *LIKE print("{:*<5}".format("LIKE")) #左对齐 输出 LIKE* print("{:*^10}".format("LIKE")) #居中 输出 ***LIKE*** def function(): return "LIKE" print(f"年龄:{age},姓名:{name}") #调用变量 输出 年龄:17,姓名:LIKE print(f"年龄:{list[0]},姓名:{list[1]}") #调用列表元素 输出 年龄:17,姓名:LIKE print(f"年龄:{tuple[0]},姓名:{tuple[1]}") #调用元组元素 输出 年龄:17,姓名:LIKE print(f"年龄:{dict['age']},姓名:{dict['name']}") #调用字典元素 输出 年龄:17,姓名:LIKE print(f"{1+1}") #调用并计算表达式 输出 2 print(f"姓名:{function()}") #调用并计算函数 输出 姓名:LIKE
Copy after login

The above is the detailed content of How to write python output statement. 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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!