msg = 'hello'   #本质:name = str('任意形式内容')# msg = 'hello'   # msg = str('hello')
# print(type(msg))
<class 'str'>msg = 'hello world'
print(msg[0])
print(msg[6])
h
wmsg = 'hello world'
print(msg[-1])
dmsg[0] = 'H'    #会提示报错msg='hello world'
res=msg[1:7] #从下标1(也就是第2个字符)开始取,取到下标7-1(第7个字符),空格占1个字符串
print(res)
ello wmsg='hello world'
res=msg[0:5:2] # 从下标0(也就是第1个字符)开始取,取到下标6-1(第5个字符),步长为2
print(res) 
hlomsg='hello world'
res=msg[5:0:-1] # 从下标5(也就是第6个字符)开始取,取到下标0(第1个字符),步长为-1
print(res)
 ollemsg='hello world'
res=msg[:] # 相当于res=msg[0:11]
print(res)
hello worldmsg='hello world'
res=msg[::-1] # 把字符串倒过来
print(res)
dlrow ollehmsg='hello world'
res = len(msg)  # len用于计算字符串的长度,输出的类型为str
print(msg,type(msg))
hello world <class 'str'>print("alex" in "alex is sb")
Trueprint("alex" not in "alex is sb")
Falseprint(not "alex" in "alex is sb") # 不推荐使用
Falsemsg='      egon      '
res=msg.strip()
print(msg) # 不会改变原值
print(res) # 是产生了新值
      egon      
egonmsg='****egon****'
print(msg.strip('*'))
egonmsg='**/*=-**egon**-=()**'
print(msg.strip('*/-=()'))
egonmsg='****e*****gon****'
print(msg.strip('*'))
e*****goninp_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
emsg='***egon****'
print(msg.strip('*'))   # 可以移除字符串中指定的符号msg='***egon****'
# print(msg.strip('*'))
print(msg.lstrip('*'))
egon****msg='***egon****'
print(msg.rstrip('*'))
***egonmsg='AbbbCCCC'
print(msg.lower())
abbbccccmsg='AbbbCCCC'
print(msg.upper())
ABBBCCCCprint("alex is sb".startswith("alex"))
Trueprint("alex is sb".endswith('sb'))
Trueres='我的名字是 {} 我的年龄是 {}'.format('egon',18)
print(res)
我的名字是 egon 我的年龄是 18info="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:malemsg="you can you up no can no bb"
print(msg.replace("you","YOU",))
YOU can YOU up no can no bbmsg="you can you up no can no bb"
print(msg.replace("you","YOU",1))
YOU can you up no can no bbprint('123'.isdigit())
Trueprint('12.3'.isdigit())
Falseage=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
6print(msg.index('e'))
print(msg.index('egon'))
1
6print(msg.find('xxx')) # 返回-1,代表找不到
-1print(msg.index('xxx')) # 抛出异常
    print(msg.index('xxx')) # 抛出异常
ValueError: substring not foundmsg='hello egon hahaha egon、 egon'
print(msg.count('egon'))
3print('egon'.center(50,'*'))
***********************egon***********************print('egon'.ljust(50,'*'))
egon**********************************************print('egon'.rjust(50,'*'))
**********************************************egonprint('egon'.zfill(10))
000000egonmsg='hello\tworld'
print(msg.expandtabs(2)) # 设置制表符代表的空格数为2
hello worldprint("hello world egon".capitalize())
Hello world egonprint("Hello WorLd EGon".swapcase())
hELLO wORlD egONprint("hello world egon".title())
Hello World Egonprint('123'.isdigit())
Trueprint('abc'.islower())
Trueprint('ABC'.isupper())
Trueprint('Hello World'.istitle())
Trueprint('123123aadsf'.isalnum()) # 字符串由字母或数字组成结果为True
Trueprint('ad'.isalpha()) # 字符串由由字母组成结果为True
Trueprint('     '.isspace()) # 字符串由空格组成结果为True
Trueprint('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
Falseprint(num2.isnumeric()) # True
print(num3.isnumeric()) # True
print(num4.isnumeric()) # True
True
True
Trueprint(num2.isdecimal()) # True
print(num3.isdecimal()) # False
print(num4.isdecimal()) # False
True
False
False原文:https://www.cnblogs.com/xuexianqi/p/12456287.html