msg = 'hello' #本质:name = str('任意形式内容')
# msg = 'hello' # msg = str('hello')
# print(type(msg))
<class 'str'>
msg = 'hello world'
print(msg[0])
print(msg[6])
h
w
msg = 'hello world'
print(msg[-1])
d
msg[0] = 'H' #会提示报错
msg='hello world'
res=msg[1:7] #从下标1(也就是第2个字符)开始取,取到下标7-1(第7个字符),空格占1个字符串
print(res)
ello w
msg='hello world'
res=msg[0:5:2] # 从下标0(也就是第1个字符)开始取,取到下标6-1(第5个字符),步长为2
print(res)
hlo
msg='hello world'
res=msg[5:0:-1] # 从下标5(也就是第6个字符)开始取,取到下标0(第1个字符),步长为-1
print(res)
olle
msg='hello world'
res=msg[:] # 相当于res=msg[0:11]
print(res)
hello world
msg='hello world'
res=msg[::-1] # 把字符串倒过来
print(res)
dlrow olleh
msg='hello world'
res = len(msg) # len用于计算字符串的长度,输出的类型为str
print(msg,type(msg))
hello world <class 'str'>
print("alex" in "alex is sb")
True
print("alex" not in "alex is sb")
False
print(not "alex" in "alex is sb") # 不推荐使用
False
msg=' egon '
res=msg.strip()
print(msg) # 不会改变原值
print(res) # 是产生了新值
egon
egon
msg='****egon****'
print(msg.strip('*'))
egon
msg='**/*=-**egon**-=()**'
print(msg.strip('*/-=()'))
egon
msg='****e*****gon****'
print(msg.strip('*'))
e*****gon
inp_user=input('your name>>: ').strip() # inp_user=" egon"
inp_pwd=input('your password>>: ').strip()
if inp_user == 'egon' and inp_pwd == '123':
print('登录成功')
else:
print('账号密码错误')
info='egon 18 male'
res=info.split()
print(res)
['egon', '18', 'male']
info='egon:18:male'
res=info.split(':')
print(res)
['egon', '18', 'male']
info='egon:18:male'
res=info.split(':',0) # 分隔次数为0,就是不分隔
print(res)
['egon:18:male']
info='egon:18:male'
res=info.split(':',1)
print(res)
['egon', '18:male']
info='egon:18:male'
for x in info:
print(x)
e
g
o
n
:
1
8
:
m
a
l
e
msg='***egon****'
print(msg.strip('*')) # 可以移除字符串中指定的符号
msg='***egon****'
# print(msg.strip('*'))
print(msg.lstrip('*'))
egon****
msg='***egon****'
print(msg.rstrip('*'))
***egon
msg='AbbbCCCC'
print(msg.lower())
abbbcccc
msg='AbbbCCCC'
print(msg.upper())
ABBBCCCC
print("alex is sb".startswith("alex"))
True
print("alex is sb".endswith('sb'))
True
res='我的名字是 {} 我的年龄是 {}'.format('egon',18)
print(res)
我的名字是 egon 我的年龄是 18
info="egon:18:male"
print(info.split(':',1))
['egon', '18:male']
info="egon:18:male"
print(info.rsplit(':',1))
["egon:18","male"]
l=['egon', '18', 'male']
res=l[0]+":"+l[1]+":"+l[2]
res=":".join(l)
print(res)
egon:18:male
msg="you can you up no can no bb"
print(msg.replace("you","YOU",))
YOU can YOU up no can no bb
msg="you can you up no can no bb"
print(msg.replace("you","YOU",1))
YOU can you up no can no bb
print('123'.isdigit())
True
print('12.3'.isdigit())
False
age=input('请输入你的年龄:').strip()
if age.isdigit():
age=int(age) # int("abbab")
if age > 18:
print('猜大了')
elif age < 18:
print('猜小了')
else:
print('才最了')
else:
print('必须输入数字,傻子')
print(msg.find('e')) # 返回要查找的字符串在大字符串中的起始索引
print(msg.find('egon'))
1
6
print(msg.index('e'))
print(msg.index('egon'))
1
6
print(msg.find('xxx')) # 返回-1,代表找不到
-1
print(msg.index('xxx')) # 抛出异常
print(msg.index('xxx')) # 抛出异常
ValueError: substring not found
msg='hello egon hahaha egon、 egon'
print(msg.count('egon'))
3
print('egon'.center(50,'*'))
***********************egon***********************
print('egon'.ljust(50,'*'))
egon**********************************************
print('egon'.rjust(50,'*'))
**********************************************egon
print('egon'.zfill(10))
000000egon
msg='hello\tworld'
print(msg.expandtabs(2)) # 设置制表符代表的空格数为2
hello world
print("hello world egon".capitalize())
Hello world egon
print("Hello WorLd EGon".swapcase())
hELLO wORlD egON
print("hello world egon".title())
Hello World Egon
print('123'.isdigit())
True
print('abc'.islower())
True
print('ABC'.isupper())
True
print('Hello World'.istitle())
True
print('123123aadsf'.isalnum()) # 字符串由字母或数字组成结果为True
True
print('ad'.isalpha()) # 字符串由由字母组成结果为True
True
print(' '.isspace()) # 字符串由空格组成结果为True
True
print('print'.isidentifier())
print('age_of_egon'.isidentifier())
print('1age_of_egon'.isidentifier())
True
True
False # 变量名不能以数字开头
num1=b'4' #bytes
num2=u'4' #unicode,python3中无需加u就是unicode
num3='四' #中文数字
num4='Ⅳ' #罗马数字
print(num1.isdigit()) # True
print(num2.isdigit()) # True
print(num3.isdigit()) # False
print(num4.isdigit()) # False
True
True
False
False
print(num2.isnumeric()) # True
print(num3.isnumeric()) # True
print(num4.isnumeric()) # True
True
True
True
print(num2.isdecimal()) # True
print(num3.isdecimal()) # False
print(num4.isdecimal()) # False
True
False
False
原文:https://www.cnblogs.com/xuexianqi/p/12456287.html