day08-字符串类型

字符串类型

1、作用
2、定义

msg='hello' # msg=str('msg')
print(type(msg))
<class 'str'>

3、类型转换
str可以把任意其他类型都转成字符串

res=str({'a':1})
print(res,type(res))
{'a': 1} <class 'str'>

4、使用:内置方法
4.1 优先掌握
4.1.1、按索引取值(正向取+反向取) :只能取

msg='hello world'
# 正向取
print(msg[0])
print(msg[5])
# 反向取
print(msg[-1])
结果如下: h d

只能取
msg[0]='H'

4.1.2、切片:索引的拓展应用,从一个大字符串中拷贝出一个子字符串

msg='hello world'
# 顾头不顾尾
res=msg[0:5] #x    是拷贝一个新的出来,并没有改变原来的字符串
print(res)
print(msg)
hello
hello world
# 步长
msg='hello world'
res=msg[0:5:2] # 0 2 4
print(res) # hlo
hlo
# 反向步长(了解)
res=msg[5:0:-1]
print(res) #" olle"
 olle
msg='hello world'
res=msg[:] # res=msg[0:11]
print(res)
hello world
res=msg[::-1] # 把字符串倒过来
print(res)
dlrow olleh

4.1.3、长度len

msg='hello world'
print(len(msg))
11

4.1.4、成员运算in和not in
判断一个子字符串是否存在于一个大字符串中

print("alex" in "alex is sb")
print("alex" not in "alex is sb")
print(not "alex" in "alex is sb") # 不推荐使用

True
False
False

4.1.5、移除字符串左右两侧的符号strip
默认去掉的空格

msg=' egon '
res=msg.strip()
print(msg) # 不会改变原值
print(res) # 是产生了新值

 egon 
egon

改变默认去掉的空格

msg='****egon****'
print(msg.strip('*'))
egon

了解:strip只取两边,不去中间

msg='****e*****gon****'
print(msg.strip('*'))
e
*****gon msg='**/*=-**egon**-=()**' print(msg.strip('*/-=()'))
egon

应用

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('账号密码错误')
    
    
your name>>:    egon
your password>>:    123
登录成功

4.1.6、切分split:把一个字符串按照某种分隔符进行切分,得到一个列表
  默认分隔符是空格

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(':',1)   #  在第一个冒号处进行分隔
print(res)

['egon', '18:male']

4.1.7、循环

info='egon:18:male'
for x in info:
    print(x)
e
g
o
n
:
1
8
:
m
a
l
e

4.2 需要掌握

4.2.1、strip,lstrip,rstrip

msg='***egon****'
print(msg.strip('*'))   # 两边切
print(msg.lstrip('*'))  # 左边切
print(msg.rstrip('*'))  # 右边切

egon
egon****
***egon

4.2.2、lower,upper

msg='AbbbCCCC'
print(msg.lower())   # 全改成小写字母
print(msg.upper())  # 全改成大写字母

abbbcccc
ABBBCCCC

4.2.3、startswith,endswith

print("alex is sb".startswith("alex"))  # 判断是不是以alex开头
print("alex is sb".endswith('sb'))       # 判断是不是以sb结尾

True
True

4.2.4、format

4.2.5、split,rsplit:将字符串切成列表

info="egon:18:male"
print(info.split(':',1)) # ["egon","18:male"]
print(info.rsplit(':',1)) # ["egon:18","male"]

['egon', '18:male']
['egon:18', 'male']

4.2.6、join: 把列表拼接成字符串

l=['egon', '18', 'male']
# res=l[0]+":"+l[1]+":"+l[2] # 这种方式也可以,太麻烦,不推荐
res=":".join(l) # 按照某个分隔符号,把元素全为字符串的列表拼接成一个大字符串
print(res)

egon:18:male
l=[1,"2",'aaa']    # 像这种不能拼接的就会报错
res = ":".join(l)
print(res)

Traceback (most recent call last):
  File "H:/s14-python/test.py", line 4, in <module>
    res = ":".join(l)
TypeError: sequence item 0: expected str instance, int found

4.2.7、replace 替换

msg="you can you up no can no bb"
print(msg.replace("you","YOU",))     # 未指定数量,替换所有
print(msg.replace("you","YOU",1))   # 指定数量1,只替换1个

YOU can YOU up no can no bb
YOU can you up no can no bb

4.2.8、isdigit

判断字符串是否由纯数字组成

print('123'.isdigit())   
print('12.3'.isdigit())

True
False
age=input('请输入你的年龄:').strip()
if age.isdigit():
    age=int(age) # int("abbab")
    if age > 18:
        print('猜大了')
    elif age < 18:
        print('猜小了')
    else:
        print('才最了')
else:
    print('必须输入数字,傻子')

请输入你的年龄:12.3
必须输入数字,傻子

4.3了解
4.3.1、find,rfind,index,rindex,count

msg='hello egon hahaha'

# 找到返回起始索引
print(msg.find('e')) # 返回要查找的字符串在大字符串中的起始索引
print(msg.find('egon'))
print(msg.index('e'))
print(msg.index('egon'))
1
6
1
6

# 找不到
print(msg.find('xxx')) # 返回-1,代表找不到
-1
print(msg.index('xxx')) # 抛出异常
    print(msg.index('xxx')) # 抛出异常
ValueError: substring not found

这里find是统计查找的字符的数量为多少,且找不到的话只会返回一个-1

而使用index的话一样是查找数量 ,但是找不到会抛出异常,所以推荐使用find

count 统计数量 

msg='hello egon hahaha egon、 egon'
print(msg.count('egon'))
3

4.3.2、center,ljust,rjust,zfill  

print('egon'.center(50,'*'))  #居中
print('egon'.ljust(50,'*'))     #左补
print('egon'.rjust(50,'*'))    #右补
print('egon'.zfill(10))          #前面零补

***********************egon***********************
egon**********************************************
**********************************************egon
000000egon

4.3.3、expandtabs

msg='hello	world'
print(msg)
print(msg.expandtabs(2)) # 设置制表符代表的空格数为2

hello    world
hello world

4.3.4、captalize,swapcase,title

print("hello world egon".capitalize())   # 修改第一个单词首字母为大写
print("Hello WorLd EGon".swapcase()) # 大小写互换
print("hello world egon".title())           # 第个单词的首字母修改为大写
单词 是以一个空格判断为一个单词的
Hello world egon
hELLO wORlD egON
Hello World Egon

4.3.5、is数字系列
4.3.6、is其他

print('abc'.islower())  # 是否全为小写
print('ABC'.isupper()) # 是否全为大写
print('Hello World'.istitle()) # 是否每个单词首字母为大写
print('123123aadsf'.isalnum()) # 字符串由字母或数字组成结果为True
print('ad'.isalpha()) # 字符串由由字母组成结果为True
print('     '.isspace()) # 字符串由空格组成结果为True
print('print'.isidentifier())     # 是否符合定义
print('age_of_egon'.isidentifier())  # 是否符合定义
print('1age_of_egon'.isidentifier())  # 是否符合定义


True
True
True
True
True
True
True
True
False

Process finished with exit code 0

num1=b'4' #bytes
num2=u'4' #unicode,python3中无需加u就是unicode
num3='四' #中文数字
num4='Ⅳ' #罗马数字

# isdigit只能识别:num1、num2
# print(num1.isdigit()) # True
# print(num2.isdigit()) # True
# print(num3.isdigit()) # False
# print(num4.isdigit()) # False

# isnumberic可以识别:num2、num3、num4
# print(num2.isnumeric()) # True
# print(num3.isnumeric()) # True
# print(num4.isnumeric()) # True

# isdecimal只能识别:num2
print(num2.isdecimal()) # True
print(num3.isdecimal()) # False
print(num4.isdecimal()) # False

原文地址:https://www.cnblogs.com/xiao-zang/p/12456508.html