2022年 11月 7日

python中print打印输出

①使用print直接打印输出。

例:message=‘I love python’

        print(message)

print(message.title()) 将单词首字母大写输出

print(message.upper())将单词全部大写输出

print(message.lower())将单词全部小写输出

 ②使用%占位符输出。

%s 字符串   %d表示整数,%f表示小数(默认保留小数点后6位,%.2f保留两位小数)

例: name=‘西门大官人’

age=60

print(‘My name is %s,I am %d years old’%(name,age))

③format格式化输出(推荐)

print(“…{索引}, … , {索引}, …”.format(值1, 值2))

例:name=‘西门大官人’

age=60

 print(‘My name is {0},I am {1} years old.’.format(name,age))