python字符串
字符串是一个有序的字符的集合,用于存储和表示基本的文本信息,’ ‘或’’ ‘’或’’’ ‘’’中间包含的内容称之为字符串
创建:
s = 'Hello,Eva!How are you?'
特性:
- 按照从左到右的顺序定义字符集合,下标从0开始顺序访问,有序
- 可以进行切片操作
- 不可变,字符串是不可变的,不能像列表一样修改其中某个元素,所有对字符串的修改操作其实都是相当于生成了一份新数据。
补充:
1.字符串的单引号和双引号都无法取消特殊字符的含义,如果想让引号内所有字符均取消特殊意义,在引号前面加r,如name=r’l\thf’
字符串的常用操作
字符串操作方法有非常多,但有些不常用 ,我们只讲重要的一些给大家,其它100年都用不上的有兴趣可以自己研究

def capitalize(self):首字母大写def casefold(self):把字符串全变小写>> > c = 'Alex Li'>> > c.casefold()'alex li'def center(self, width, fillchar=None):>> > c.center(50, "-")'---------------------Alex Li----------------------'def count(self, sub, start=None, end=None):"""S.count(sub[, start[, end]]) -> int>>> s = "welcome to apeland">>> s.count('e')3>>> s.count('e',3)2>>> s.count('e',3,-1)2def encode(self, encoding='utf-8', errors='strict'):"""编码,日后讲def endswith(self, suffix, start=None, end=None):>> > s = "welcome to apeland">> > s.endswith("land") 判断以什么结尾Truedef find(self, sub, start=None, end=None):"""S.find(sub[, start[, end]]) -> intReturn the lowest index in S where substring sub is found,such that sub is contained within S[start:end]. Optionalarguments start and end are interpreted as in slice notation.Return -1 on failure."""return 0def format(self, *args, **kwargs): # known special case of str.format>> > s = "Welcome {0} to Apeland,you are No.{1} user.">> > s.format("Eva", 9999)'Welcome Eva to Apeland,you are No.9999 user.'>> > s1 = "Welcome {name} to Apeland,you are No.{user_num} user.">> > s1.format(name="Alex", user_num=999)'Welcome Alex to Apeland,you are No.999 user.'def format_map(self, mapping):"""S.format_map(mapping) -> strReturn a formatted version of S, using substitutions from mapping.The substitutions are identified by braces ('{' and '}')."""讲完dict再讲这个def index(self, sub, start=None, end=None):"""S.index(sub[, start[, end]]) -> intReturn the lowest index in S where substring sub is found,such that sub is contained within S[start:end]. Optionalarguments start and end are interpreted as in slice notation.Raises ValueError when the substring is not found."""def isdigit(self):"""S.isdigit() -> boolReturn True if all characters in S are digitsand there is at least one character in S, False otherwise."""return Falsedef islower(self):"""S.islower() -> boolReturn True if all cased characters in S are lowercase and there isat least one cased character in S, False otherwise."""def isspace(self):"""S.isspace() -> boolReturn True if all characters in S are whitespaceand there is at least one character in S, False otherwise."""def isupper(self):"""S.isupper() -> boolReturn True if all cased characters in S are uppercase and there isat least one cased character in S, False otherwise."""def join(self, iterable):"""S.join(iterable) -> strReturn a string which is the concatenation of the strings in theiterable. The separator between elements is S.""">>> n = ['alex','jack','rain']>>> '|'.join(n)'alex|jack|rain'def ljust(self, width, fillchar=None):"""S.ljust(width[, fillchar]) -> strReturn S left-justified in a Unicode string of length width. Padding isdone using the specified fill character (default is a space)."""return ""def lower(self):"""S.lower() -> strReturn a copy of the string S converted to lowercase."""return ""def lstrip(self, chars=None):"""S.lstrip([chars]) -> strReturn a copy of the string S with leading whitespace removed.If chars is given and not None, remove characters in chars instead."""return ""def replace(self, old, new, count=None):"""S.replace(old, new[, count]) -> strReturn a copy of S with all occurrences of substringold replaced by new. If the optional argument count isgiven, only the first count occurrences are replaced."""return ""def rjust(self, width, fillchar=None):"""S.rjust(width[, fillchar]) -> strReturn S right-justified in a string of length width. Padding isdone using the specified fill character (default is a space)."""return ""def rsplit(self, sep=None, maxsplit=-1):"""S.rsplit(sep=None, maxsplit=-1) -> list of stringsReturn a list of the words in S, using sep as thedelimiter string, starting at the end of the string andworking to the front. If maxsplit is given, at most maxsplitsplits are done. If sep is not specified, any whitespace stringis a separator."""return []def rstrip(self, chars=None):"""S.rstrip([chars]) -> strReturn a copy of the string S with trailing whitespace removed.If chars is given and not None, remove characters in chars instead."""return ""def split(self, sep=None, maxsplit=-1):"""S.split(sep=None, maxsplit=-1) -> list of stringsReturn a list of the words in S, using sep as thedelimiter string. If maxsplit is given, at most maxsplitsplits are done. If sep is not specified or is None, anywhitespace string is a separator and empty strings areremoved from the result."""return []def startswith(self, prefix, start=None, end=None):"""S.startswith(prefix[, start[, end]]) -> boolReturn True if S starts with the specified prefix, False otherwise.With optional start, test S beginning at that position.With optional end, stop comparing S at that position.prefix can also be a tuple of strings to try."""return Falsedef strip(self, chars=None):"""S.strip([chars]) -> strReturn a copy of the string S with leading and trailingwhitespace removed.If chars is given and not None, remove characters in chars instead."""return ""def swapcase(self):"""S.swapcase() -> strReturn a copy of S with uppercase characters converted to lowercaseand vice versa."""return ""def upper(self):"""S.upper() -> strReturn a copy of S converted to uppercase."""return ""def zfill(self, width):"""S.zfill(width) -> strPad a numeric string S with zeros on the left, to fill a fieldof the specified width. The string S is never truncated."""return ""


浙公网安备 33010602011771号