当前位置:网站首页>NP3 formatted output (I)
NP3 formatted output (I)
2022-06-29 08:22:00 【Spiritual wanderer】
'''
NP3 Format output ( One )
Niu Niu 、 Niu Mei and Niu cola are Nowcoder Study Python Language , Now give them one of the three names name,
Suppose the input is name by Niuniu, The output I am Niuniu and I am studying Python in Nowcoder!
Please output the corresponding English sentence according to the above sentence pattern .
'''
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 Usage of <<<'''
# Basic usage
# 1. according to {} The order of matches the values in parentheses
s = '{} is a {}, and have worked in this company for {} years.'.format('keven','worker',1.5)
print(s)
# 2. Match parameters by index
s = 'the name of {1} is {0}'.format('keven', 'teacher')
print(s)
# 3.1 Match parameters by parameter name
s = '{name} is a {job}'.format(name='keven',job='teacher')
print(s)
# 3.2 Empathy , If the parameter has been determined , Can be used directly {} Format reference
# With f The beginning indicates that... Within braces is supported in the string python expression This usage 3.6 Later support
name = 'keven'
job = 'teacher'
s = f'{name} is a {job}'
print(s)
# 4.1 By index , Parameter names to mix and match
s = 'My name is {} , I am {} years old , and work as an {job}'.format('keven',27,job='Electronic Engineer')
print(s)
# It should be noted that , The named parameter must be written last . Responsible for compiling and reporting errors
# Advanced usage
# 1. Through the properties of the object
class Names():
name1='keven'
name2='tom'
s = 'hello {n.name1} ! I am {n.name2}'.format(n=Names)
print(s)
# 2. Support partial reference to parameters
s = 'The word is {s} , {s[0]} is initials'.format(s='world')
print(s)
# 3. Digital processing
# How to use format Keep two decimal places ? Need to use :.2f, In use % When formatting, we use %:.2f
s = 'π is {:.2f}'.format(3.1415926)
print(s)
s1 = 'π is %.2f'%3.1415926
print(s)
# String interception
s = '{:.3}'.format('Hello')
print(s)
# Put a thousand after the number
s = '{:,}'.format(888888888)
print(s)
# Binary conversion of numbers
# 10 Base number -> 2 Base number
s = '{:b}'.format(10)
print(s)
# 10 Base number -> 8 Base number
s = '{:o}'.format(10)
print(s)
# 10 Base number -> 16 Base number
s = '{:x}'.format(10)
s1 = '{:X}'.format(10)
print(s,s1)
# 4. Filling of characters
# It can be done by : Symbol ^ Numbers are used to fill the string . The number is the total length of the filled string .
s = '{:-^40}'.format('Hello World!')
print(s)
print('-'*40)
# 5. list、tuple The split
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}
# Dictionaries need to use ** To break up
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
边栏推荐
- 《动手学深度学习》(一)-- 线性神经网络
- Audio and video development cases 99 lectures - Contents
- [quantitative investment system]django realizes screening and paging from the database
- Product security - small vulnerabilities cause big problems
- Using method and de duplication of distinct() in laravel
- What does Ali's 211 mean?
- 城通网盘仿蓝奏网盘源码 附带视频教程
- 为什么两个执行很快的SQL,union之后特别慢
- 消息中间件:pulsar
- Matlab 用法
猜你喜欢

Un voyage profond d'IA dans Huawei Cloud

消息中间件:pulsar

802.11--802.11n协议 PHY

Mongodb- connect to the database using the mongo/mongosh command line
![[eye of depth wuenda machine learning homework class phase IV] regularization regularization summary](/img/24/3d0b892c0eaa330f0c69764de5da13.png)
[eye of depth wuenda machine learning homework class phase IV] regularization regularization summary

穿越过后,她说多元宇宙真的存在

一个高频问题,三种模型思维来破解此风控难题

Talking about Nacos configuration center from Nacos client

Friends, static keywords, static methods, and relationships between objects

新版付费塔罗牌测算源码 (附带搭建教程)
随机推荐
Django - installing mysqlclient error: mysqlclient 1.4.0 or newer is required; you have 0.9.3
为什么两个执行很快的SQL,union之后特别慢
NLP annotation tool: label studio realizes multi-user collaborative marking
Voice annotation tool: Praat
[domain penetration authorization] cve-2020-1472 Netlogon privilege escalation vulnerability
Reflection perfectionism
表格背单词的方法
C compiler - implicit function declaration
Time operation - time format conversion
Want to open a stock account, is it safe to open a stock account online-
Verilog first experience
[eye of depth Wu Enda's fourth operation class] summary of multiple linear regression with multiple variables
智能硬件evt dvt pvt mp
NOR flash application layer operation
[eye of depth wuenda machine learning operation class phase IV] logic regression programming implementation
一个高频问题,三种模型思维来破解此风控难题
Swift中@dynamicMemberLookup和callAsFunction特性实现对象透明代理功能
PostgreSQL installation: the database cluster initialization failed, stack hbuilder installation
SQL Server enable CDC
关于#sql#的问题:创建一个名为View_XB视图,功能是①如果有重名的视图先删除后创建②显示XSB表中本班级的男女生各有多少人,并添加检查约束