1. 字符串的格式化:
1.1占位符:%s,%d
例1:name = "wuye"
age = 22
print("my name is %s,my age is %s"%(name,age,)) # %s 既可以接收字母,也能接收数字
print("my name is %s,my age is %d"%(name,age,)) # %d 只能接收数字
print("my name is %s"%name) # 如果只有一个参数可以省略括号
例2:用户信息展示
name=input("name:") age=input("age:") sex=input("sex:") height=input("height:") msg=\'\'\' ------------%s info----------- name:%s age:%s sex:%s height:%s ------------------------------ \'\'\'%(name,name,age,sex,heigth,) # 使用占位符记得在末尾加一个,号表示结束 print(msg)
1.2 format 格式化
format基本格式与占位符不同地方在于:%s 换为 {},字符串后面跟 .format()。
例1:
>>>name = "wuye"
>>>age = 22
>>>print("my name is {},my age is {}".format(name,age)) # 不设置指定位置,按默认位置进行传参
# 输出结果为:
my name is wuye,my age is 22
还可以设置位置进行传参
例2:
>>>name = "wuye"
>>>age = 22
>>>print("my name is {0},my age is {1}".format(name,age)) # 设置指定位置,按位置进行传参
# 输出结果为:
my name is wuye,my age is 22
>>>
>>>
>>>print("my name is {1},my age is {0}".format(name,age)) # 设置指定位置
# 输出结果为:
my name is 22,my age is wuye
>>>print("my age is {1},my name is {0},my age is {1}".format(name,age)) # 设置指定位置
# 输出结果
my age is 22,my name is wuye,my age is 22
还可以通过关键字进行传参
例3:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
print("my name is {name}, my age is {age}".format(name="wuye", age=22))
# 通过字典设置参数
site = {"name": "wuye", "age": 22}
print("my name is {name}, my age is {age}".format(**kwargs))
# 通过列表索引设置参数
my_list = [\'wuye\', 22]
print("my name is {0[0]}, my age is {0[1]}".format(my_list)) # "0" 是必须的
# 输出结果全部为:
my name is wuye, my age is 22
1.3 数字格式化
>>>print("{:.2f}".format(3.141592653))
3.14
| 数字 | 格式 | 输出 | 描述 |
|---|---|---|---|
| 3.1415926 | {:.2f} | 3.14 | 保留小数点后两位 |
| 3.1415926 | {:+.2f} | +3.14 | 带符号保留小数点后两位 |
| -1 | {:+.2f} | -1.00 | 带符号保留小数点后两位 |
| 2.71828 | {:.0f} | 3 | 不带小数 |
| 5 | {:0>2d} | 05 | 数字补零 (填充左边, 宽度为2) |
| 5 | {:x<4d} | 5xxx | 数字补x (填充右边, 宽度为4) |
| 10 | {:x<4d} | 10xx | 数字补x (填充右边, 宽度为4) |
| 1000000 | {:,} | 1,000,000 | 以逗号分隔的数字格式 |
| 0.25 | {:.2%} | 25.00% | 百分比格式 |
| 1000000000 | {:.2e} | 1.00e+09 | 指数记法 |
| 13 | {:>10d} | 13 | 右对齐 (默认, 宽度为10) |
| 13 | {:<10d} | 13 | 左对齐 (宽度为10) |
| 13 | {:^10d} | 13 | 中间对齐 (宽度为10) |
| 11 |
\'{:b}\'.format(11)
\'{:d}\'.format(11)
\'{:o}\'.format(11)
\'{:x}\'.format(11)
\'{:#x}\'.format(11)
\'{:#X}\'.format(11)
|
1011
11
13
b
0xb
0XB
|
进制 |
^, <, > 分别是居中、左对齐、右对齐,后面带宽度, : 号后面带填充的字符,只能是一个字符,不指定则默认是用空格填充。
+ 表示在正数前显示 +,负数前显示 -; (空格)表示在正数前加空格
b、d、o、x 分别是二进制、十进制、八进制、十六进制。
此外我们可以使用大括号 {} 来转义大括号,如下实例:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
print ("{} 对应的位置是 {{0}}".format("runoob"))
2. 字符串常用的的方法
字符串的所有方法如图:
下面说明常用方法及示例:
# name=\'egon\' #name=str(\'egon\')
# print(type(name))
# ############### 优先掌握 ###############
# --------------- 1.rstrip() / lstrip() / 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\')
# --------------- 2.split() / rsplit() 切割 ---------------
message = \'小黑现在一脸懵逼,因为昨天晚上学习,到深夜\'
result = message.split(\',\',1) #从左到右切割第一个字符
result1 = message.rsplit(\',\',1) #从右到左切割第一个字符
print(result)
print(result1)
# 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]))
# --------------- 3.长度len ---------------
# print(len(\'hell 123\'))
# --------------- 4.索引 ---------------
# 切片:切出子字符串
# msg=\'hello world\'
# print(msg[1:3]) #1 2
# print(msg[1:4]) #1 2 3
# --------------- 5 .upper() / .lower() 字符串大小写转换 ---------------
# value = \'alex\'
# new1 = value.upper() # 变大写
# new2 = value.lower() # 变小写
# 用处:验证码判断
check_code = \'IyUf\'
code = input(\'请输入验证码%s:\'%(check_code,))
print(code)
if code.lower() == check_code.lower():
print(\'输入正确\')
# --------------- 6. startswith,endswith 以什么 开头/结尾 ---------------
# name=\'wye_NB\'
# print(name.endswith(\'NB\'))
# print(name.startswith(\'wye\'))
# --------------- 7. 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\' %(\'wuye\',22,\'male\'))
# print(\'my name is {} my age is {} my sex is {}\'.format(\'wuye\',2,\'2male\'))
# print(\'my name is {0} my age is {1} my sex is {0}:
{2}\'.format(\'wuye\',22,\'male\'))
# print(\'my name is {name} my age is {age} my sex is {sex}\'.format(
# sex=\'wuye\',
# age=22,
# name=\'egon\'))
# --------------- 8. 索引查找,计数---------------
# name=\'goee say hello\'
# # print(name.find(\'S\',1,3)) #顾头不顾尾,找不到则返回-1不会报错,找到了则显示索引
# # print(name.index(\'S\')) #同上,但是找不到会报错
#
# print(name.count(\'S\',1,5)) #顾头不顾尾,如果不指定范围则查找所有,找到了返回元素个数
# --------------- 9. join ---------------
# info=\'root:x:0:0::/root:/bin/bash\'
# print(info.split(\':\'))
# l=[\'root\', \'x\', \'0\', \'0\', \'\', \'/root\', \'/bin/bash\']
# print(\':\'.join(l))
# --------------- 10. isdicimal 判断是否是整数 ---------------
# name = \'1\'
# print(name.isdecimal()) # 只能判断字符串中的元素
# ############### 了解部分 ###############
# --------------- expandtabs
# name=\'egon\thello\'
# print(name)
# print(name.expandtabs(1))
# --------------- center,ljust,rjust,zfill
# name=\'egon\'
# print(name.center(30,\'-\')) # 居中,左右用-填充,长度为30
# print(name.ljust(30,\'*\')) # 文字靠左,右边用*填充,长度为30
# print(name.rjust(30,\'*\')) # 文字靠右,左边用*填充,长度为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())