当前位置:网站首页>NP3 格式化输出(一)
NP3 格式化输出(一)
2022-06-29 07:23:00 【心灵流浪者】
'''
NP3 格式化输出(一)
牛牛、牛妹和牛可乐正在Nowcoder学习Python语言,现在给定他们三个当中的某一个名字name,
假设输入的name为Niuniu,则输出 I am Niuniu and I am studying Python in Nowcoder!
请按以上句式输出相应的英文句子。
'''
print('=== Method 1 ===')
name = input('Please input name: ')
print("I am {} and I am studying Python in Nowcoder!".format(name))
print('=== Method 2 ===')
print('I am ' + name + ' and I am studying Python in Nowcoder!')
print('=== Method 3 ===')
print('I am %s and I am studying Python in Nowcoder!'%name)
print('=== Method4 ===')
print(f'I am {name} and I am studying Python in Nowcoder!')
'''>>> format 的用法 <<<'''
#基础用法
# 1. 按照{}的顺序依次匹配括号中的值
s = '{} is a {}, and have worked in this company for {} years.'.format('keven','worker',1.5)
print(s)
# 2. 通过索引的方式去匹配参数
s = 'the name of {1} is {0}'.format('keven', 'teacher')
print(s)
# 3.1通过参数名来匹配参数
s = '{name} is a {job}'.format(name='keven',job='teacher')
print(s)
# 3.2同理,如果参数已经确定,可以直接利用{}进行格式化引用
# 以f开头表示在字符串中支持大括号内的python表达式 此用法3.6之后支持
name = 'keven'
job = 'teacher'
s = f'{name} is a {job}'
print(s)
# 4.1 可以通过索引,参数名来混搭进行匹配
s = 'My name is {} , I am {} years old , and work as an {job}'.format('keven',27,job='Electronic Engineer')
print(s)
#需要注意的是,命名参数必须写道最后。负责会编译报错
#进阶用法
# 1. 通过对象的属性
class Names():
name1='keven'
name2='tom'
s = 'hello {n.name1} ! I am {n.name2}'.format(n=Names)
print(s)
# 2.支持对参数部分引用
s = 'The word is {s} , {s[0]} is initials'.format(s='world')
print(s)
# 3. 数字的处理
# 如何使用format 保留两位小数呢? 需要使用:.2f,在用%进行格式化时我们使用的是%:.2f
s = 'π is {:.2f}'.format(3.1415926)
print(s)
s1 = 'π is %.2f'%3.1415926
print(s)
#字符串截取
s = '{:.3}'.format('Hello')
print(s)
#给数字后面加千位符
s = '{:,}'.format(888888888)
print(s)
#数字的进制转换
# 10进制 -> 2进制
s = '{:b}'.format(10)
print(s)
# 10进制 -> 8进制
s = '{:o}'.format(10)
print(s)
# 10进制 -> 16进制
s = '{:x}'.format(10)
s1 = '{:X}'.format(10)
print(s,s1)
# 4. 字符的填充
# 可通过:符号^数字进行字符串的填充。 其中数字为填充后的字符串总长度。
s = '{:-^40}'.format('Hello World!')
print(s)
print('-'*40)
# 5. list、tuple的拆分
foods = ['fish', 'beef', 'fruit']
s = 'i like eat {} and {} and {}'.format(*foods)
# i like eat fish and beef and fruit
print(s)
foods = ['fish', 'beef', 'fruit']
s = 'i like eat {2} and {0} and {1}'.format(*foods)
# i like eat fruit and fish and beef
print(s)
dict_temp = {'name': 'Lily', 'age': 18}
# 字典需要用 ** 进行拆分
s = 'My name is {name}, i am {age} years old'.format(**dict_temp)
print(s) # My name is Lily, i am 18 years old
# reference link : https://blog.csdn.net/qq_42855293/article/details/118480087
边栏推荐
- Soft restart
- 关于#sql#的问题:创建一个名为View_XB视图,功能是①如果有重名的视图先删除后创建②显示XSB表中本班级的男女生各有多少人,并添加检查约束
- [hcie TAC] question 5-2
- 常见的七大排序
- Process communication - Pipeline
- 阿里的211是指什么?
- 壁纸小程序源码双端微信抖音小程序
- [Kerberos] analysis of Kerberos authentication
- [eye of depth wuenda machine learning operation class phase IV] logic regression programming implementation
- Voice processing tool: Sox
猜你喜欢

Protobuf binary file learning and parsing

Use GPU training in the cloud on the laboratory (take yolov5 as an example)

What are the organizational structure and job responsibilities of product managers in Internet companies?

SizeBalanceTree

新版付费塔罗牌测算源码 (附带搭建教程)

SQL Server enable CDC

Matlab 用法

A review of visual SLAM methods for autonomous driving vehicles

U盘内存卡数据丢失怎么恢复,这样操作也可以

语音合成:概述【不等长序列关系建模的生成任务】
随机推荐
[repair collection function, update login interface] knowledge payment applet, blog applet, full version open source code, resource realization applet, with 299 whole station resource data
Mongodb- connect to the database using the mongo/mongosh command line
Mosaic notes
关于SqlSugar的多对多的级联插入的问题(无法获取集合属性的id,导致无法维护中间表)
Debugging nocturnal simulator with ADB command
Ceres optimization based on sophus
Notes mosaïque
Robotframework learning notes: introduction to robot framework and browserlibrary (playwright)
壁纸小程序源码双端微信抖音小程序
Flutter file read / write -path_ provider
Simple use of AWS elastic Beanstalk
SizeBalanceTree
laravel 中 distinct() 的使用方法与去重
Hands on deep learning (I) -- linear neural network
Paddlenlp general information extraction model: UIE [information extraction {entity relationship extraction, Chinese word segmentation, accurate entity markers, emotion analysis, etc.}, text error cor
Behaviortree in ros2
802.11--802.11n协议 PHY
About the many to many cascading insertion of sqlsugar (the ID of the collection attribute cannot be obtained, so the intermediate table cannot be maintained)
Protobuf binary file learning and parsing
Segment tree and use