字符串格式化及字符串的一些方法

1.%s,%d

举例1:name='egon'

     age=20

     print("my name is %s  my age is %s" %(name,age))#%s既能接受字符串,也能接受数字

     print(‘my name is %s  my age is %d’ %(name,age))#%d只能接受数字

举例2:用户信息的显示

 1 while True:
 2     name=input("name:")
 3     age=input("age:")
 4     sex=input("sex:")
 5     height=input("height:")
 6     msg='''
 7              ------------%s info-----------
 8              name:%s
 9              age:%s
10              sex:%s
11              height:%s
12              ------------------------------
13         '''%(name,name,age,sex,heigth)
14     print(msg)

运行结果如下:

 

 2.字符串方法

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# name='egon' #name=str('egon')
# print(type(name))
 
 
#优先掌握
#1.移除空白strip
# msg='             hello         '
# print(msg)
# print(msg.strip())
# 移除‘*’
# msg='***hello*********'
# msg=msg.strip('*')
# print(msg)
#移除左边的
# print(msg.lstrip('*'))
#移除右边的
# print(msg.rstrip('*'))
 
#用处
while  True :
     name = input ( 'user: ' ).strip()
     password = input ( 'password: ' ).strip()
     if  name  = =  'egon'  and  password  = =  '123' :
         print ( 'login successfull' )
 
 
 
#切分split
# info='root:x:0:0::/root:/bin/bash'
# print(info[0]+info[1]+info[2]+info[3])
 
# user_l=info.split(':')
# print(user_l[0])
 
# msg='hello world egon say hahah'
# print(msg.split()) #默认以空格作为分隔符
 
#cmd='download|xhp.mov|3000'
# cmd_l=cmd.split('|')
# print(cmd_l[1])
# print(cmd_l[0])
# print(cmd.split('|',1))
 
#用处
while  True :
     cmd = input ( '>>: ' ).strip()
     if  len (cmd)  = =  0 : continue
     cmd_l = cmd.split()
     print ( '命令是:%s 命令的参数是:%s'  % (cmd_l[ 0 ],cmd_l[ 1 ]))
 
 
 
 
 
#长度len
# print(len('hell 123'))
 
 
#索引
# 切片:切出子字符串
# msg='hello world'
# print(msg[1:3]) #1 2
# print(msg[1:4]) #1 2 3
 
 
 
# 掌握部分
oldboy_age = 84
while  True :
     age = input ( '>>: ' ).strip()
     if  len (age)  = =  0 :
         continue
     if  age.isdigit():
         age = int (age)
     else :
         print ( 'must be int' )
 
 
 
 
 
#startswith,endswith
# name='alex_SB'
# print(name.endswith('SB'))
# print(name.startswith('alex'))
 
 
#replace
# name='alex say :i have one tesla,my name is alex'
# print(name.replace('alex','SB',1))
 
# print('my name is %s my age is %s my sex is %s' %('egon',18,'male'))
# print('my name is {} my age is {} my sex is {}'.format('egon',18,'male'))
# print('my name is {0} my age is {1} my sex is {0}:
{ 2 } '.format(' egon ',18,' male'))
# print('my name is {name} my age is {age} my sex is {sex}'.format(
#     sex='male',
#     age=18,
#     name='egon'))
 
 
# name='goee say hello'
# # print(name.find('S',1,3)) #顾头不顾尾,找不到则返回-1不会报错,找到了则显示索引
# # print(name.index('S')) #同上,但是找不到会报错
#
# print(name.count('S',1,5)) #顾头不顾尾,如果不指定范围则查找所有
 
 
#join
# info='root:x:0:0::/root:/bin/bash'
# print(info.split(':'))
 
# l=['root', 'x', '0', '0', '', '/root', '/bin/bash']
# print(':'.join(l))
 
 
#lower,upper
# name='eGon'
# print(name.lower())
# print(name.upper())
 
 
#了解部分
#expandtabs
# name='egon\thello'
# print(name)
# print(name.expandtabs(1))
 
 
#center,ljust,rjust,zfill
# name='egon'
# # print(name.center(30,'-'))
# print(name.ljust(30,'*'))
# print(name.rjust(30,'*'))
# print(name.zfill(50)) #用0填充
 
 
#captalize,swapcase,title
# name='eGon'
# print(name.capitalize()) #首字母大写,其余部分小写
# print(name.swapcase()) #大小写翻转
# msg='egon say hi'
# print(msg.title()) #每个单词的首字母大写
 
 
#在python3中
num0 = '4'
num1 = b '4'  #bytes
num2 = u '4'  #unicode,python3中无需加u就是unicode
num3 = '四'  #中文数字
num4 = 'Ⅳ'  #罗马数字
 
 
#isdigt:str,bytes,unicode
# print(num0.isdigit())
# print(num1.isdigit())
# print(num2.isdigit())
# print(num3.isdigit())
# print(num4.isdigit())
 
#isdecimal:str,unicode
# num0='4'
# num1=b'4' #bytes
# num2=u'4' #unicode,python3中无需加u就是unicode
# num3='四' #中文数字
# num4='Ⅳ' #罗马数字
# print(num0.isdecimal())
# # print(num1.)
# print(num2.isdecimal())
# print(num3.isdecimal())
# print(num4.isdecimal())
 
#isnumeric:str,unicode,中文,罗马
# num0='4'
# num1=b'4' #bytes
# num2=u'4' #unicode,python3中无需加u就是unicode
# num3='四' #中文数字
# num4='Ⅳ' #罗马数字
#
# print(num0.isnumeric())
# # print(num1)
# print(num2.isnumeric())
# print(num3.isnumeric())
# print(num4.isnumeric())
 
 
 
 
#is其他
# name='egon123'
# print(name.isalnum()) #字符串由字母和数字组成
# name='asdfasdfa sdf'
# print(name.isalpha()) #字符串只由字母组成
#
 
# name='asdfor123'
# print(name.isidentifier())
name = 'egGon'
print (name.islower())
# print(name.isupper())
# print(name.isspace())
name = 'Egon say'
print (name.istitle())
 
                        

  

转载于:https://www.cnblogs.com/xiaohema/p/8452907.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Python中,字符串格式化是通过将变量的值插入到字符串中来创建新字符串的过程。以下是几种常见的字符串格式化方法: 1. 使用占位符 在字符串中使用占位符(例如%s和%d)来表示变量的值,然后通过字符串的format()方法将这些占位符替换为实际的值。例如: ``` name = "Alice" age = 30 print("My name is %s and I am %d years old." % (name, age)) ``` 输出结果为: My name is Alice and I am 30 years old. 2. 使用f-strings f-strings是Python 3.6及更高版本引入的一种格式化字符串方法。它们允许在字符串中嵌入表达式,并在字符串中使用大括号{}表示变量的值。例如: ``` name = "Alice" age = 30 print(f"My name is {name} and I am {age} years old.") ``` 输出结果为: My name is Alice and I am 30 years old. 3. 使用format()方法 format()方法可以接受任意数量的参数,并使用花括号{}来表示变量的值。例如: ``` name = "Alice" age = 30 print("My name is {} and I am {} years old.".format(name, age)) ``` 输出结果为: My name is Alice and I am 30 years old. 4. 使用模板字符串 模板字符串是一种包含占位符的字符串,可以使用字符串模板库(Template)中的substitute()方法将占位符替换为实际的值。例如: ``` from string import Template name = "Alice" age = 30 template = Template("My name is $name and I am $age years old.") print(template.substitute(name=name, age=age)) ``` 输出结果为: My name is Alice and I am 30 years old.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值