python字符串归纳
其他
2019-03-12 22:16:22
阅读次数: 0
“”“字符串”""
可以存在重复字符,空格与符号也算字符
a = 123
b = ‘123’
c = “123”
d = ‘’‘123’’’
e = “xiaoming speaks:‘i love python’”
print(type(a))
print(type(b))
print(type©)
print(type(d))
if c== b:
print(‘相等’)
else:
print(‘不相等’)
A = 123
print(‘A’)
print(A)
str_new='hello python,hello word
index查找指定字符串,查找到返回(第一个字符的下标)索引值,否则报错
ret1=str_new.index(‘w’hi’)
find查找指定字符串,查找到返回(第一个字符的下标)索引值,否则返回为-1
ret3=str_new.find(‘python’)
print(ret3)
ret4=str_new.find(‘hi’)
print(ret4)
count查找指定字符串,查找到返回需要查找到的个数
ret5=str_new.count(‘i’)
print(ret5)
replace替换将旧的字符串替换成新的字符串,如果原来的字符串中没有指定的字符串,返回原来的字符串,如果指定了替换的次数,则替换指定个数的旧字符串,数字表示第几个替换对象
ret6=str_new.replace(‘hello’,‘hi’,1)
print(ret6)
split以指定字符串(字符)为分隔符,分隔符切片(非常重要),返回的是一个列表
ret7=str_new.split(‘hello’)
print(ret7)
ret71=str_new.split(’ ',maxsplit=1)#maxsplit最大分隔数
print(ret71)
右对齐,左对齐,以及居中对齐,可以指定填充的值,但是一般空格填充
ret72=‘hello’
ret73=ret72.rjust(10)
ret74=ret72.ljust(10)
ret75=ret72.center(10)
print(ret73)
print(ret74)
print(ret75)
去空白字符(去空格),或者指定去左边的空格或者右边的空格
ret10 = ret9.strip()
print(ret10)
ret9.lstrip()
ret9.rstrip()
print(’#########################’)
for i in str2:
print(i)
capitalize首字母大写
ret8=str_new.capitalize()
print(ret8)
title字符串每个单词的首字母大写
ret9=str_new.title()
print(ret9)
startswith检测以什么开头,布尔值返回结果
ret10=str_new
转载自blog.csdn.net/qq_44090577/article/details/88391656