08---基本数据类型--字符串内置方法

一。定义

msg = 'hello world!'  # msg = str(msg)

二。类型转换

# str可以把任意其他类型都转成字符串
res=str({'a':1})
print(res,type(res))

三。内置方法

  1、按索引取值(正向取,反向取):只能取

msg='hello world'
# 正向取
print(msg[0])
print(msg[5])
# 反向取
print(msg[-1])

  2、切片:索引扩展应用从一个大字符串拷贝出小字符串

msg='hello world'
# 顾头不顾尾
res=msg[0:5] 
print(res)
print(msg)
# 步长
res=msg[0:5:2] # 0 2 4
print(res) # hlo

# 反向步长(了解)
res=msg[5:0:-1]
print(res) #" olle"

res=msg[::-1] # 把字符串倒过来
print(res)

  3、长度len

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

  4、成员运算in 和 not in

# in 判断hello 是否在 str1里面
>>> 'hello' in str1  
True
# not in:判断tony 是否不在 str1里面
>>> 'tony' not in str1 
True

  5、strip

# 括号内不指定字符,默认移除首尾空白字符(空格、\n、\t)
>>> str1 = '  life is short!  '
>>> str1.strip()  
life is short!

# 括号内指定字符,移除首尾指定的字符
>>> str2 = '**tony**'  
>>> str2.strip('*')  
tony

  6、split

#  括号内不指定字符,默认以空格作为切分符号
>>> str3='hello world'
>>> str3.split()
['hello', 'world']
#  括号内指定分隔字符,则按照括号内指定的字符切割字符串
>>> str4 = '127.0.0.1'
>>> str4.split('.')  
['127', '0', '0', '1']  # 注意:split切割得到的结果是列表数据类型

  7、循环遍历

>>> str5 = '今天你好吗?'
>>> for line in str5:  # 依次取出字符串中每一个字符
...     print(line)
...
今
天
你
好
吗
?

四。需要掌握的操作

  1、strip lstrip  rstrip

>>> str1 = '**tony***'

>>> str1.strip('*')  # 移除左右两边的指定字符
'tony'
>>> str1.lstrip('*')  # 只移除左边的指定字符
tony***
>>> str1.rstrip('*')  # 只移除右边的指定字符
**tony

  2、lower upper

>>> str2 = 'My nAme is tonY!'
>>> str2.lower()  # 将英文字符串全部变小写
my name is tony!
>>> str2.upper()  # 将英文字符串全部变大写
MY NAME IS TONY!

  3、startswith endswith

>>> str3 = 'tony jam'
# startswith()判断字符串是否以括号内指定的字符开头,结果为布尔值True或False
>>> str3.startswith('t') 
True
>>> str3.startswith('j')
False
# endswith()判断字符串是否以括号内指定的字符结尾,结果为布尔值True或False
>>> str3.endswith('jam')
True
>>> str3.endswith('tony')  
False

  4、格式化输出format,参见https://www.cnblogs.com/Kathrine/p/12421267.html

  5、split rsplit

# split会按照从左到右的顺序对字符串进行切分,可以指定切割次数
>>> str5='C:/a/b/c/d.txt'
>>> str5.split('/',1)
['C:', 'a/b/c/d.txt']  

# rsplit刚好与split相反,从右往左切割,可以指定切割次数
>>> str5='a|b|c'
>>> str5.rsplit('|',1)
['a|b', 'c']

  6、join

# 从可迭代对象中取出多个字符串,然后按照指定的分隔符进行拼接,拼接的结果为字符串
>>> '%'.join('hello') # 从字符串'hello'中取出多个字符串,然后按照%作为分隔符号进行拼接
'h%e%l%l%o'
>>> '|'.join(['tony','18','read'])  # 从列表中取出多个字符串,然后按照*作为分隔符号进行拼接
'tony|18|read'

  7、replace

# 用新的字符替换字符串中旧的字符
>>> str7 = 'my name is tony, my age is 18!'  # 将tony的年龄由18岁改成73岁
>>> str7 = str7.replace('18', '73')  # 语法:replace('旧内容', '新内容')
>>> str7
my name is tony, my age is 73!

# 可以指定修改的个数
>>> str7 = 'my name is tony, my age is 18!'
>>> str7 = str7.replace('my', 'MY',1) # 只把一个my改为MY
>>> str7
'MY name is tony, my age is 18!'

  8、isdigit

# 判断字符串是否是纯数字组成,返回结果为True或False
>>> str8 = '5201314'
>>> str8.isdigit()
True

>>> str8 = '123g123'
>>> str8.isdigit()
False

五。了解内容

# 1.find,rfind,index,rindex,count
# 1.1 find:从指定范围内查找子字符串的起始索引,找得到则返回数字1,找不到则返回-1
>>> msg='tony say hello'
>>> msg.find('o',1,3)  # 在索引为1和2(顾头不顾尾)的字符中查找字符o的索引
1  
# 1.2 index:同find,但在找不到时会报错
>>> msg.index('e',2,4) # 报错ValueError
# 1.3 rfind与rindex:略
# 1.4 count:统计字符串在大字符串中出现的次数
>>> msg = "hello everyone"
>>> msg.count('e')  # 统计字符串e出现的次数
4
>>> msg.count('e',1,6)  # 字符串e在索引1~5范围内出现的次数
1

# 2.center,ljust,rjust,zfill
>>> name='tony'
>>> name.center(30,'-')  # 总宽度为30,字符串居中显示,不够用-填充
-------------tony-------------
>>> name.ljust(30,'*')  # 总宽度为30,字符串左对齐显示,不够用*填充
tony**************************
>>> name.rjust(30,'*')  # 总宽度为30,字符串右对齐显示,不够用*填充
**************************tony
>>> name.zfill(50)  # 总宽度为50,字符串右对齐显示,不够用0填充
0000000000000000000000000000000000000000000000tony

# 3.expandtabs
>>> name = 'tony\thello'  # \t表示制表符(tab键)
>>> name
tony    hello
>>> name.expandtabs(1)  # 修改\t制表符代表的空格数
tony hello

# 4.captalize,swapcase,title
# 4.1 captalize:首字母大写
>>> message = 'hello everyone nice to meet you!'
>>> message.capitalize()
Hello everyone nice to meet you!  
# 4.2 swapcase:大小写翻转
>>> message1 = 'Hi girl, I want make friends with you!'
>>> message1.swapcase()  
hI GIRL, i WANT MAKE FRIENDS WITH YOU!  
#4.3 title:每个单词的首字母大写
>>> msg = 'dear my friend i miss you very much'
>>> msg.title()
Dear My Friend I Miss You Very Much 

# 5.is数字系列
#在python3中
num1 = b'4' #bytes
num2 = u'4' #unicode,python3中无需加u就是unicode
num3 = '' #中文数字
num4 = '' #罗马数字

#isdigt:bytes,unicode
>>> num1.isdigit()
True
>>> num2.isdigit()
True
>>> num3.isdigit()
False
>>> num4.isdigit() 
False

#isdecimal:uncicode(bytes类型无isdecimal方法)
>>> num2.isdecimal() 
True
>>> num3.isdecimal() 
False
>>> num4.isdecimal() 
False

#isnumberic:unicode,中文数字,罗马数字(bytes类型无isnumberic方法)
>>> num2.isnumeric() 
True
>>> num3.isnumeric() 
True
>>> num4.isnumeric() 
True

# 三者不能判断浮点数
>>> num5 = '4.3'
>>> num5.isdigit()
False
>>> num5.isdecimal()
False
>>> num5.isnumeric()
False

'''
总结:
    最常用的是isdigit,可以判断bytes和unicode类型,这也是最常见的数字应用场景
    如果要判断中文数字或罗马数字,则需要用到isnumeric。
'''

# 6.is其他
>>> name = 'tony123'
>>> name.isalnum() #字符串中既可以包含数字也可以包含字母
True
>>> name.isalpha() #字符串中只包含字母
False
>>> name.isidentifier()
True
>>> name.islower()  # 字符串是否是纯小写
True
>>> name.isupper()  # 字符串是否是纯大写
False
>>> name.isspace()  # 字符串是否全是空格
False
>>> name.istitle()  # 字符串中的单词首字母是否都是大写
False

 

posted @ 2020-03-10 16:25  微信搜索-程序媛小庄  阅读(155)  评论(0编辑  收藏  举报