常见方法调用:
常用的字符串方法(method):
方法的调用语法:
对象.方法名(方法传参)
说明:
方法的调用同函数调用一样属于表达式。
示例:
'abc'.isalpha() # 判断'abc'是否全为英文字母
123.isalpha() # 错的,123没有isalpha方法
常用方法见:
python_base_docs_html/str_180408102847.html
示例:
center(总长度,’补充的字符’)
S.center(sub,....)
>>> "ABC".center(10)
' ABC '
>>> "ABC".center(20, '#')
'########ABC#########'
>>>S=”hellp world”
count找个数
变量名.count(‘查找的内容’[, start][, end])
>>> S.count('ell')
1
>>> S.count('o')
2
>>> S.count('o', 6) #第六位后面的o有几个
1
find找位置
>>>S.find(’wor’)
6
>>>S.find(’ll’)
2
>>> S.find('llabcd') #找不到时返回-1
-1
>>> S = "ABC123.txt"
startswith以什么开头
>>> S.startswith('ABC')
True
>>> S.startswith('hello')
False
endswith以什么结尾
>>> S.endswith('.txt')
True
>>> S.endswith('.mp3')
False
改变大小写upper、lower
>>>S=”hello world”
>>>S.upper()
“HELLO WORLD”
>>>S.lower()
“hello world”
去掉空白部分strip
S=” \r\n hello world \t\n”
S.strip()剪切掉空白字符的开始位置和结束位置重点的都保留包括’ ’
S.rstrip()左侧剪掉空格
S.lstrip()右侧剪掉空格
s = input("按q退出")
if s.strip().lower() == "q":
print("程序退出")
替换原来字符串replace
S.replace(old,new[,count])
title改成标题格式(把每个字符串的第一个字母改为大写)
S.title()