1.upper():将字符串中所有的字符都转换为大写
message = "Hello Word"
print ( message. upper( ) )
2.lower():将字符串中所有的字符都转换为小写
print ( message. lower( ) )
3.startswith(xxx):判断字符串的内容是否以xxx开头,区分大小写
print ( message. startswith( "Hello" ) )
print ( message. startswith( "hello" ) )
print ( message. startswith( "H" ) )
print ( message. startswith( " H" ) )
4.endswith(xxx):判断字符串的内容是否以xxx结尾,区分大小写
print ( message. endswith( "Word" ) )
print ( message. endswith( "word" ) )
print ( message. endswith( "d" ) )
print ( message. endswith( "d " ) )
5.isdigit():判断字符串的内容是否是纯数字组成的
msg_str1 = "123"
print ( msg_str1. isdigit( ) )
msg_str2 = "50元"
print ( msg_str2. isdigit( ) )
6.strip():去除字符串中所有的空格
msg_str3 = " 张同学 "
print ( msg_str3)
print ( msg_str3. strip( ) )
7.split(xxx):按某个字符xxx分割字符串,默认按空格分割
citys = "北京 上海 深圳 石家庄 天津"
city_list = citys. split( )
print ( city_list)
citys1 = "无锡,重庆,成都,苏州,杭州"
city_list1 = citys1. split( "," )
print ( city_list1)
8.join():按指定的字符将多个字符串拼接在一起
city_new = "-" . join( city_list1)
print ( city_new)
9.find():从字符串串中搜索指定的字符串,没有匹配的返回-1,有匹配的返回第一个匹配内容的索引位置
print ( message. find( "l" ) )
print ( message. find( "s" ) )
10.index():从字符串串中搜索指定的字符串,没有匹配的抛异常,有匹配的返回第一个匹配内容的索引位置
print ( message. index( "l" ) )
11.count():统计指定字符串在目标字符串中出现的次数
print ( message. count( "l" ) )
print ( message. count( "s" ) )
12.replace():将目标字符串中指定的内容替换为新内容,count参数:替换的次数,默认全替换
print ( message. replace( "l" , "s" ) )
print ( message. replace( "l" , "s" , 1 ) )