Python筑基之旅-字符串(一)

05-28 1027阅读

目录

一、字符串

1、字符串的定义

1-1、使用单引号(')

1-2、使用双引号(") 

1-3、使用三引号('''或""") 

1-4、原始字符串(r'str'或R'str') 

2、字符串的语法

3、获取字符串的属性和方法

4、获取字符串的帮助信息

5、字符串的用法

5-1、capitalize()方法

5-2、casefold()方法

5-3、center()方法

5-4、count()方法

5-5、encode()方法

5-6、endswith()方法 

5-7、expandtabs()方法

5-8、find()方法

5-9、format()方法

5-10、format_map()方法

5-11、index()方法

5-12、isalnum()方法

5-13、isalpha()方法

5-14、isascii()方法

5-15、isdigit()方法

二、推荐阅读

1、Python-VBA函数之旅-str()函数

2、Python算法之旅

3、Python函数之旅

4、个人主页-神奇夜光杯-CSDN

Python筑基之旅-字符串(一)

Python筑基之旅-字符串(一)

Python筑基之旅-字符串(一)

一、字符串

1、字符串的定义

        在Python中,字符串(Str)是一种数据类型,用于存储一系列字符(文本),字符串可以包含字母、数字、标点符号和特殊字符等。

        在Python中,可以通过以下几种方式定义字符串:

1-1、使用单引号(')
s1 = 'Hello, my Python World!'
print(type(s1)) # 输出:
1-2、使用双引号(") 
s1 = "Hello, my Python World!"
print(type(s1)) # 输出:
1-3、使用三引号('''或""") 
# 使用三个单引号:'''
s1 = '''Hello, my Python World!'''
print(type(s1)) # 输出:
# 使用三个双引号:"""
s1 = """Hello, my Python World!"""
print(type(s1)) # 输出:
1-4、原始字符串(r'str'或R'str') 
# 用r'str'
path1 = r'C:\Users\Username\Documents\file.txt'
print(type(path1))  # 输出:
# 用r"str"
path2 = r"C:\Users\Username\Documents\file.txt"
print(type(path2))  # 输出:
# 用r'''str'''
path3 = r'''C:\Users\Username\Documents\file.txt'''
print(type(path3))  # 输出:
# 用r"""str"""
path4 = r"""C:\Users\Username\Documents\file.txt"""
print(type(path4))  # 输出:
# 用R'str'
path5 = R'C:\Users\Username\Documents\file.txt'
print(type(path5))  # 输出:
# 用R"str"
path6 = R"C:\Users\Username\Documents\file.txt"
print(type(path6))  # 输出:
# 用R'''str'''
path7 = R'''C:\Users\Username\Documents\file.txt'''
print(type(path7))  # 输出:
# 用R"""str"""
path8 = R"""C:\Users\Username\Documents\file.txt"""
print(type(path8))  # 输出:

2、字符串的语法

        Python中的字符串是由单引号(')、双引号(")或三引号(''' 或 """)括起来的字符或文本,字符串可以是ASCII字符、Unicode字符或两者都有。

3、获取字符串的属性和方法

        用dir()函数获取str所有属性和方法的列表

print(dir(str))
# ['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__',
# '__getattribute__', '__getitem__', '__getnewargs__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__',
# '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', 
# '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 
# 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 
# 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 
# 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'removeprefix', 'removesuffix', 'replace', 'rfind', 'rindex', 
# 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

4、获取字符串的帮助信息

        用help()函数获取str的帮助信息

Help on class str in module builtins:
class str(object)
 |  str(object='') -> str
 |  str(bytes_or_buffer[, encoding[, errors]]) -> str
 |  
 |  Create a new string object from the given object. If encoding or
 |  errors is specified, then the object must expose a data buffer
 |  that will be decoded using the given encoding and error handler.
 |  Otherwise, returns the result of object.__str__() (if defined)
 |  or repr(object).
 |  encoding defaults to sys.getdefaultencoding().
 |  errors defaults to 'strict'.
 |  
 |  Methods defined here:
 |  
 |  __add__(self, value, /)
 |      Return self+value.
 |  
 |  __contains__(self, key, /)
 |      Return key in self.
 |  
 |  __eq__(self, value, /)
 |      Return self==value.
 |  
 |  __format__(self, format_spec, /)
 |      Return a formatted version of the string as described by format_spec.
 |  
 |  __ge__(self, value, /)
 |      Return self>=value.
 |  
 |  __getattribute__(self, name, /)
 |      Return getattr(self, name).
 |  
 |  __getitem__(self, key, /)
 |      Return self[key].
 |  
 |  __getnewargs__(...)
 |  
 |  __gt__(self, value, /)
 |      Return self>value.
 |  
 |  __hash__(self, /)
 |      Return hash(self).
 |  
 |  __iter__(self, /)
 |      Implement iter(self).
 |  
 |  __le__(self, value, /)
 |      Return self bool
 |      
 |      Return True if S ends with the specified suffix, False otherwise.
 |      With optional start, test S beginning at that position.
 |      With optional end, stop comparing S at that position.
 |      suffix can also be a tuple of strings to try.
 |  
 |  expandtabs(self, /, tabsize=8)
 |      Return a copy where all tab characters are expanded using spaces.
 |      
 |      If tabsize is not given, a tab size of 8 characters is assumed.
 |  
 |  find(...)
 |      S.find(sub[, start[, end]]) -> int
 |      
 |      Return the lowest index in S where substring sub is found,
 |      such that sub is contained within S[start:end].  Optional
 |      arguments start and end are interpreted as in slice notation.
 |      
 |      Return -1 on failure.
 |  
 |  format(...)
 |      S.format(*args, **kwargs) -> str
 |      
 |      Return a formatted version of S, using substitutions from args and kwargs.
 |      The substitutions are identified by braces ('{' and '}').
 |  
 |  format_map(...)
 |      S.format_map(mapping) -> str
 |      
 |      Return a formatted version of S, using substitutions from mapping.
 |      The substitutions are identified by braces ('{' and '}').
 |  
 |  index(...)
 |      S.index(sub[, start[, end]]) -> int
 |      
 |      Return the lowest index in S where substring sub is found,
 |      such that sub is contained within S[start:end].  Optional
 |      arguments start and end are interpreted as in slice notation.
 |      
 |      Raises ValueError when the substring is not found.
 |  
 |  isalnum(self, /)
 |      Return True if the string is an alpha-numeric string, False otherwise.
 |      
 |      A string is alpha-numeric if all characters in the string are alpha-numeric and
 |      there is at least one character in the string.
 |  
 |  isalpha(self, /)
 |      Return True if the string is an alphabetic string, False otherwise.
 |      
 |      A string is alphabetic if all characters in the string are alphabetic and there
 |      is at least one character in the string.
 |  
 |  isascii(self, /)
 |      Return True if all characters in the string are ASCII, False otherwise.
 |      
 |      ASCII characters have code points in the range U+0000-U+007F.
 |      Empty string is ASCII too.
 |  
 |  isdecimal(self, /)
 |      Return True if the string is a decimal string, False otherwise.
 |      
 |      A string is a decimal string if all characters in the string are decimal and
 |      there is at least one character in the string.
 |  
 |  isdigit(self, /)
 |      Return True if the string is a digit string, False otherwise.
 |      
 |      A string is a digit string if all characters in the string are digits and there
 |      is at least one character in the string.
 |  
 |  isidentifier(self, /)
 |      Return True if the string is a valid Python identifier, False otherwise.
 |      
 |      Call keyword.iskeyword(s) to test whether string s is a reserved identifier,
 |      such as "def" or "class".
 |  
 |  islower(self, /)
 |      Return True if the string is a lowercase string, False otherwise.
 |      
 |      A string is lowercase if all cased characters in the string are lowercase and
 |      there is at least one cased character in the string.
 |  
 |  isnumeric(self, /)
 |      Return True if the string is a numeric string, False otherwise.
 |      
 |      A string is numeric if all characters in the string are numeric and there is at
 |      least one character in the string.
 |  
 |  isprintable(self, /)
 |      Return True if the string is printable, False otherwise.
 |      
 |      A string is printable if all of its characters are considered printable in
 |      repr() or if it is empty.
 |  
 |  isspace(self, /)
 |      Return True if the string is a whitespace string, False otherwise.
 |      
 |      A string is whitespace if all characters in the string are whitespace and there
 |      is at least one character in the string.
 |  
 |  istitle(self, /)
 |      Return True if the string is a title-cased string, False otherwise.
 |      
 |      In a title-cased string, upper- and title-case characters may only
 |      follow uncased characters and lowercase characters only cased ones.
 |  
 |  isupper(self, /)
 |      Return True if the string is an uppercase string, False otherwise.
 |      
 |      A string is uppercase if all cased characters in the string are uppercase and
 |      there is at least one cased character in the string.
 |  
 |  join(self, iterable, /)
 |      Concatenate any number of strings.
 |      
 |      The string whose method is called is inserted in between each given string.
 |      The result is returned as a new string.
 |      
 |      Example: '.'.join(['ab', 'pq', 'rs']) -> 'ab.pq.rs'
 |  
 |  ljust(self, width, fillchar=' ', /)
 |      Return a left-justified string of length width.
 |      
 |      Padding is done using the specified fill character (default is a space).
 |  
 |  lower(self, /)
 |      Return a copy of the string converted to lowercase.
 |  
 |  lstrip(self, chars=None, /)
 |      Return a copy of the string with leading whitespace removed.
 |      
 |      If chars is given and not None, remove characters in chars instead.
 |  
 |  partition(self, sep, /)
 |      Partition the string into three parts using the given separator.
 |      
 |      This will search for the separator in the string.  If the separator is found,
 |      returns a 3-tuple containing the part before the separator, the separator
 |      itself, and the part after it.
 |      
 |      If the separator is not found, returns a 3-tuple containing the original string
 |      and two empty strings.
 |  
 |  removeprefix(self, prefix, /)
 |      Return a str with the given prefix string removed if present.
 |      
 |      If the string starts with the prefix string, return string[len(prefix):].
 |      Otherwise, return a copy of the original string.
 |  
 |  removesuffix(self, suffix, /)
 |      Return a str with the given suffix string removed if present.
 |      
 |      If the string ends with the suffix string and that suffix is not empty,
 |      return string[:-len(suffix)]. Otherwise, return a copy of the original
 |      string.
 |  
 |  replace(self, old, new, count=-1, /)
 |      Return a copy with all occurrences of substring old replaced by new.
 |      
 |        count
 |          Maximum number of occurrences to replace.
 |          -1 (the default value) means replace all occurrences.
 |      
 |      If the optional argument count is given, only the first count occurrences are
 |      replaced.
 |  
 |  rfind(...)
 |      S.rfind(sub[, start[, end]]) -> int
 |      
 |      Return the highest index in S where substring sub is found,
 |      such that sub is contained within S[start:end].  Optional
 |      arguments start and end are interpreted as in slice notation.
 |      
 |      Return -1 on failure.
 |  
 |  rindex(...)
 |      S.rindex(sub[, start[, end]]) -> int
 |      
 |      Return the highest index in S where substring sub is found,
 |      such that sub is contained within S[start:end].  Optional
 |      arguments start and end are interpreted as in slice notation.
 |      
 |      Raises ValueError when the substring is not found.
 |  
 |  rjust(self, width, fillchar=' ', /)
 |      Return a right-justified string of length width.
 |      
 |      Padding is done using the specified fill character (default is a space).
 |  
 |  rpartition(self, sep, /)
 |      Partition the string into three parts using the given separator.
 |      
 |      This will search for the separator in the string, starting at the end. If
 |      the separator is found, returns a 3-tuple containing the part before the
 |      separator, the separator itself, and the part after it.
 |      
 |      If the separator is not found, returns a 3-tuple containing two empty strings
 |      and the original string.
 |  
 |  rsplit(self, /, sep=None, maxsplit=-1)
 |      Return a list of the substrings in the string, using sep as the separator string.
 |      
 |        sep
 |          The separator used to split the string.
 |      
 |          When set to None (the default value), will split on any whitespace
 |          character (including \n \r \t \f and spaces) and will discard
 |          empty strings from the result.
 |        maxsplit
 |          Maximum number of splits.
 |          -1 (the default value) means no limit.
 |      
 |      Splitting starts at the end of the string and works to the front.
 |  
 |  rstrip(self, chars=None, /)
 |      Return a copy of the string with trailing whitespace removed.
 |      
 |      If chars is given and not None, remove characters in chars instead.
 |  
 |  split(self, /, sep=None, maxsplit=-1)
 |      Return a list of the substrings in the string, using sep as the separator string.
 |      
 |        sep
 |          The separator used to split the string.
 |      
 |          When set to None (the default value), will split on any whitespace
 |          character (including \n \r \t \f and spaces) and will discard
 |          empty strings from the result.
 |        maxsplit
 |          Maximum number of splits.
 |          -1 (the default value) means no limit.
 |      
 |      Splitting starts at the front of the string and works to the end.
 |      
 |      Note, str.split() is mainly useful for data that has been intentionally
 |      delimited.  With natural text that includes punctuation, consider using
 |      the regular expression module.
 |  
 |  splitlines(self, /, keepends=False)
 |      Return a list of the lines in the string, breaking at line boundaries.
 |      
 |      Line breaks are not included in the resulting list unless keepends is given and
 |      true.
 |  
 |  startswith(...)
 |      S.startswith(prefix[, start[, end]]) -> bool
 |      
 |      Return 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.
 |  
 |  strip(self, chars=None, /)
 |      Return a copy of the string with leading and trailing whitespace removed.
 |      
 |      If chars is given and not None, remove characters in chars instead.
 |  
 |  swapcase(self, /)
 |      Convert uppercase characters to lowercase and lowercase characters to uppercase.
 |  
 |  title(self, /)
 |      Return a version of the string where each word is titlecased.
 |      
 |      More specifically, words start with uppercased characters and all remaining
 |      cased characters have lower case.
 |  
 |  translate(self, table, /)
 |      Replace each character in the string using the given translation table.
 |      
 |        table
 |          Translation table, which must be a mapping of Unicode ordinals to
 |          Unicode ordinals, strings, or None.
 |      
 |      The table must implement lookup/indexing via __getitem__, for instance a
 |      dictionary or list.  If this operation raises LookupError, the character is
 |      left untouched.  Characters mapped to None are deleted.
 |  
 |  upper(self, /)
 |      Return a copy of the string converted to uppercase.
 |  
 |  zfill(self, width, /)
 |      Pad a numeric string with zeros on the left, to fill a field of the given width.
 |      
 |      The string is never truncated.
 |  
 |  ----------------------------------------------------------------------
 |  Static methods defined here:
 |  
 |  __new__(*args, **kwargs) from builtins.type
 |      Create and return a new object.  See help(type) for accurate signature.
 |  
 |  maketrans(...)
 |      Return a translation table usable for str.translate().
 |      
 |      If there is only one argument, it must be a dictionary mapping Unicode
 |      ordinals (integers) or characters to Unicode ordinals, strings or None.
 |      Character keys will be then converted to ordinals.
 |      If there are two arguments, they must be strings of equal length, and
 |      in the resulting dictionary, each character in x will be mapped to the
 |      character at the same position in y. If there is a third argument, it
 |      must be a string, whose characters will be mapped to None in the result.

5、字符串的用法

5-1、capitalize()方法
# capitalize()方法:将字符串的第一个字符转换为大写,其余字符转换为小写,且不影响原始字符串
s = "hello, my Pyton World!"
s_capitalized = s.capitalize()
print(s_capitalized)  # 输出: Hello, my pyton world!
print(s)  # 输出: hello, my Pyton World!
5-2、casefold()方法
# casefold()方法:将字符串中的所有字符转换为小写,并进行额外的折叠映射,以考虑不同语言环境的“大小写不敏感”比较,这与lower()方法有些相似,
# 但casefold()提供了更彻底的折叠,使得字符串在比较时更加“平等”
# 该方法不影响原始字符串
s = "Hello, my Python World!"
s_casefolded = s.casefold()
print(s_casefolded) # 输出:hello, my python world!
print(s)  # 输出: Hello, my Python World!
5-3、center()方法
# 1、方法:str.center
# 2、语法:str.center(width[, fillchar])
# 3、参数:
# 3-1、width(必需):整数,表示新字符串的总宽度
# 3-2、fillchar(可选):单个字符,用于填充新字符串的空白部分,默认为空格
# 4、功能:将字符串居中,并在其两侧填充指定的字符(默认为空格)
# 5、返回值:一个新的字符串
# 6、说明:
# 6-1、如果原始字符串的长度大于或等于width,则center()方法会返回原始字符串的副本
# 6-2、此方法不影响原始字符串
# 7、示例:
# 原始字符串
s = "myelsa"
# 使用center方法,总宽度为10,默认填充字符为空格
s_centered = s.center(10)
print(s_centered)  # 输出:' myelsa '
# 使用center方法,总宽度为10,指定填充字符为'*'
s_centered_with_star = s.center(10, '*')
print(s_centered_with_star)  # 输出: '**myelsa**'
# 注意原始字符串s并没有改变
print(s)  # 输出: 'myelsa'
5-4、count()方法
# 1、方法:str.count
# 2、语法:str.count(sub[, start[, end]])
# 3、参数:
# 3-1、sub:要搜索的子字符串
# 3-2、start(可选):开始搜索的索引位置;默认为0,即字符串的开始
# 3-3、end(可选):结束搜索的索引位置(不包括该位置);默认为字符串的末尾
# 4、功能:用于计算子字符串在字符串中出现的次数
# 5、返回值:一个非负整数
# 6、说明:
# 6-1、如果子字符串在字符串中不存在,则返回0
# 6-2、当使用start和end参数时,count()方法只会计算在指定范围内子字符串出现的次数(遵守左闭右开原则)
# 7、示例:
# 原始字符串
s = "hello world, hello everyone"
# 计算子字符串 "hello" 出现的次数
count_hello = s.count("hello")
# 输出结果
print(count_hello)  # 输出: 2
# 计算子字符串 "world" 出现的次数
count_world = s.count("world")
# 输出结果
print(count_world)  # 输出: 1
# 计算子字符串 "python" 出现的次数(不存在)
count_python = s.count("python")
# 输出结果
print(count_python)  # 输出: 0
# 使用start和end参数
count_hello_start_end = s.count("hello", 7, 20)  # 只搜索索引7到20之间的部分(不包括20)
# 输出结果
print(count_hello_start_end)  # 输出: 1,因为第二个 "hello" 在这个范围内
5-5、encode()方法
# 1、方法:str.encode
# 2、语法:str.encode(encoding='utf-8', errors='strict')
# 3、参数:
# 3-1、encoding:指定字符编码的名称,默认为'utf-8';Python支持多种字符编码,但最常用的是'utf-8',它能够表示任何Unicode字符
# 3-2、errors:指定如何处理编码错误。默认是'strict',表示如果无法编码某个字符,则抛出UnicodeEncodeError;
#     其他可能的值包括'ignore'(忽略无法编码的字符)、'replace'(用问号?替换无法编码的字符)和'xmlcharrefreplace'(使用XML字符引用替换无法编码的字符)
# 4、功能:用于将字符串转换为字节串(bytes)
# 5、返回值:一个字节串(bytes)
# 6、说明:
# 7、示例:
# 原始字符串
s = "Hello, World!"
# 编码为UTF-8字节串
b_utf8 = s.encode('utf-8')
# 打印字节串  
print(b_utf8)  # 输出: b'Hello, World!'
# 尝试使用不支持的编码
try:
    b_latin1 = s.encode('latin1')  # 如果字符串中只包含Latin-1字符,这将成功
    print(b_latin1) # 输出:b'Hello, World!'
except UnicodeEncodeError as e:
    print(f"编码错误: {e}")
# 使用'replace'处理编码错误
s_with_non_ascii = "Hello, 世界!"
b_with_replacement = s_with_non_ascii.encode('ascii', 'replace')
# 打印带有替换字符的字节串
print(b_with_replacement)  # 输出: b'Hello, ??!'
5-6、endswith()方法 
# 1、方法:str.endswith
# 2、语法:str.endswith(suffix[, start[, end]])
# 3、参数:
# 3-1、suffix(必须):表示要检查的后缀
# 3-2、start(可选):指定开始检查的起始位置(索引);默认为0,即字符串的开始
# 3-3、end(可选):指定结束检查的结束位置(索引);默认为字符串的长度,即字符串的末尾
# 4、功能:用于检查字符串是否以指定的后缀结束
# 5、返回值:一个布尔值
# 6、说明:如果字符串以指定的后缀结束,则返回 True,否则返回 False
# 7、示例:
# 原始字符串
s = "Hello, World!"
# 检查字符串是否以"World!"结尾
is_endswith_world = s.endswith("World!")
print(is_endswith_world)  # 输出: True
# 检查字符串是否以"Hello"结尾(不是)
is_endswith_hello = s.endswith("Hello")
print(is_endswith_hello)  # 输出: False
# 检查字符串从索引5开始到结束是否以"World!"结尾(是)
is_endswith_world_from_index5 = s.endswith("World!", 7)  # 注意:索引7是"W"的位置,但不影响结果
print(is_endswith_world_from_index5)  # 输出: True
# 检查字符串从索引0开始到索引10是否以"ello"结尾(不是)
is_endswith_ello = s.endswith("ello", 0, 11)  # 注意:索引11超出了字符串长度,但不影响检查到索引10的部分
print(is_endswith_ello)  # 输出: False
5-7、expandtabs()方法
# 1、方法:str.expandtabs
# 2、语法:str.expandtabs([tabsize=8])
# 3、参数:
# 3-1、tabsize(可选):表示一个制表符应该由多少个空格代替;默认值是8,这通常是大多数系统中制表符的标准宽度
# 4、功能:用于将字符串中的制表符(\t)替换为一定数量的空格,从而扩展它们以匹配指定的制表符宽度
# 5、返回值:返回一个新的字符串
# 6、说明:expandtabs()方法返回一个新的字符串,而原始字符串保持不变
# 7、示例:
# 使用默认的制表符宽度(8个空格)
s = "Hello\tWorld"
expanded_s = s.expandtabs()
print(expanded_s)  # 输出: Hello   World
# 使用自定义的制表符宽度(4个空格)
s = "Hello\tWorld"
expanded_s = s.expandtabs(tabsize=11)
print(expanded_s)  # 输出: Hello      World
# 字符串中有多个制表符
s = "Hello\t\tWorld"
expanded_s = s.expandtabs()
print(expanded_s)  # 输出: Hello           World
5-8、find()方法
# 1、方法:str.find
# 2、语法:str.find(sub[, start[, end]])
# 3、参数:
# 3-1、sub(必需):表示要查找的子字符串
# 3-2、start(可选):表示开始查找的位置索引;默认值为0,表示从字符串的开始位置查找
# 3-3、end(可选):表示结束查找的位置索引;默认值为字符串的长度,表示查找到字符串的末尾
# 4、功能:用于查找子字符串在主字符串中首次出现的位置索引
# 5、返回值:返回子字符串在主字符串中首次出现的位置索引
# 6、说明:
# 6-1、当指定了起始索引时,find()方法将从该索引开始查找
# 6-2、如果未找到子字符串,或者起始索引大于结束索引,则find()方法返回-1
# 7、示例:
# 查找子字符串首次出现的位置
s = "Hello, World!"
index = s.find("World")
print(index)  # 输出: 7
# 查找子字符串首次出现的位置(指定起始索引)
index = s.find("World", 6)  # 从索引6开始查找
print(index)  # 输出: 7
# 查找不存在的子字符串
index = s.find("Python")
print(index)  # 输出: -1
# 查找子字符串在指定范围内的位置
index = s.find("o", 1, 5)  # 从索引1开始到索引5结束(不包括索引5)查找'o'
print(index)  # 输出: 4
# 如果起始索引大于结束索引,find()会返回 -1
index = s.find("o", 10, 5)
print(index)  # 输出: -1
5-9、format()方法
# 在Python中,字符串的format()方法提供了一种灵活的方式来格式化字符串
# format()方法可以与字符串字面量中的大括号`{}`占位符一起使用,或者与`str.format()`方法一起使用来插入和格式化变量值
# 1、基本用法
# 使用`{}`占位符,并在调用`format()`方法时提供参数:
name = "Myelsa"
age = 18
formatted_string = "My name is {} and I am {} years old.".format(name, age)
print(formatted_string)  # 输出: My name is Myelsa and I am 18 years old.
# 2、命名参数
# 在`{}`中使用变量名来引用参数:
name = "Bruce"
formatted_string = "Hello, {name}!".format(name=name)
print(formatted_string)  # 输出: Hello, Bruce!
# 3、位置参数
# 使用位置参数,与占位符的顺序对应:
name = "Jimmy"
age = 15
formatted_string = "Name: {}, Age: {}".format(name, age)
print(formatted_string)  # 输出: Name: Jimmy, Age: 15
# 4、格式化数字
# 使用冒号`:`后的格式说明符来格式化数字:
pi = 3.141592653589793
formatted_string = "Pi is approximately {:.4f}".format(pi)
print(formatted_string)  # 输出: Pi is approximately 3.1416
# 5、格式说明符
# 5-1、`{}`:占位符
# 5-2、`:`:开始格式说明符
# 5-3、`.`(可选):用于指定小数点后的精度
# 5-4、``, `^`(可选):用于指定对齐方式(左对齐、右对齐、居中对齐)
# 5-5、`+`(可选):用于在数字前显示正负号
# 5-6、`-`(可选):用于左对齐(默认是右对齐)
# 5-7、空格(可选):在正数前加一个空格
# 5-8、`#`(可选):用于二进制、八进制、十六进制表示,以及浮点数的小数点和指数
# 5-9、`0`(可选):用于指定填充的字符(如果指定了宽度)
# 5-10、数字(可选):指定最小宽度
# 5-11类型(可选):如`f`(浮点数)、`s`(字符串)、`d`(整数)等
# 6、嵌套和复杂用法
# `format()`方法还支持更复杂的嵌套和格式化,例如访问列表、字典或对象的属性:
person = {"name": "Jimmy", "age": 15, "city": "Foshan"}
formatted_string = "My name is {name}, I am {age} years old, and I live in {city}.".format(**person)
print(formatted_string)  # 输出: My name is Jimmy, I am 15 years old, and I live in Foshan.
# 7、访问列表中的元素
items = ["apple", "banana", "cherry"]
formatted_string = "I have {} items: {}".format(len(items), ", ".join(items))
print(formatted_string)  # 输出: I have 3 items: apple, banana, cherry
# 8、f-string(Python 3.6+)
# 从Python 3.6开始,可以使用f-string(格式化字符串字面量)作为另一种更简洁、易读的字符串格式化方法:
name = "Myelsa"
age = 18
formatted_string = f"My name is {name} and I am {age} years old."
print(formatted_string)  # 输出: My name is Myelsa and I am 18 years old.
# f-string允许在字符串中直接嵌入表达式,并在运行时求值,这使得字符串格式化更加简洁和直观
5-10、format_map()方法
# 1、方法:str.format_map
# 2、语法:str.format_map(mapping)
# 3、参数:
# 3-1、mapping(必需):表示映射类型,如字典等
# 4、功能:接受一个映射类型作为参数,并使用映射类型中的键值对来替换字符串中的格式化字段
# 5、返回值:一个新的字符串
# 6、说明:注意,format_map()方法只能处理字典或其他映射类型中的键值对,与 str.format()方法相比,它提供了一种更简洁、更直观的方式来使用字典进行字符串格式化
# 7、示例:
# 定义一个包含要格式化的数据的字典
data = {
    'name': 'Myelsa',
    'age': 18,
    'city': 'Guangzhou'
}
# 定义一个带有格式化字段的字符串模板
template = 'My name is {name}, I am {age} years old, and I live in {city}.'
# 使用format_map()方法来格式化字符串
formatted_string = template.format_map(data)
# 输出格式化后的字符串
print(formatted_string) # 输出:My name is Myelsa, I am 18 years old, and I live in Guangzhou.
5-11、index()方法
# 1、方法:str.index
# 2、语法:str.index(sub[, start[, end]])
# 3、参数:
# 3-1、sub(必需):表示要查找的子字符串
# 3-2、start(可选):表示开始查找的位置索引;默认值为0,表示从字符串的开始位置查找
# 3-3、end(可选):表示结束查找的位置索引;默认值为字符串的长度,表示查找到字符串的末尾
# 4、功能:用于查找子字符串在主字符串中首次出现的位置索引
# 5、返回值:一个正整数
# 6、说明:
# 6-1、如果start参数大于end参数,或者sub参数在主字符串中不存在,index()方法会抛出一个ValueError异常;因此,在使用index()方法时,最好使用try-except块来捕获并处理可能的异常
# 6-2、与find()方法不同,index()方法在找不到子字符串时会抛出ValueError异常,而find()方法会返回-1
# 7、示例:
s = "Hello, World!"
# 查找子字符串首次出现的位置
index = s.index("World")
print(index)  # 输出: 7
# 查找子字符串首次出现的位置(指定起始索引)
index = s.index("World", 8)  # 注意:这里会抛出ValueError,因为从索引8开始找不到"world"
# print(index)  # 这行代码会抛出 ValueError: substring not found
# 查找不存在的子字符串
try:
    index = s.index("Python")
    print(index)
except ValueError:
    print("Substring not found")  # 输出: Substring not found
# 查找子字符串在指定范围内的位置
index = s.index("o", 1, 5)  # 从索引1开始到索引5结束(不包括索引5)查找'o'
print(index)  # 输出: 4
5-12、isalnum()方法
# 1、方法:str.isalnum
# 2、语法:str.isalnum()
# 3、参数:无
# 4、功能:用于检查字符串中的所有字符是否都是字母(alphabetic)或数字(numeric),并且字符串至少包含一个字符
# 5、返回值:一个布尔值
# 6、说明:
# 6-1、如果字符串中所有字符都是字母或数字,则返回True,否则返回False
# 6-2、isalnum()方法只会检查字符串中的字符是否是字母或数字,而不会考虑字符的大小写(即大写字母和小写字母都被视为字母)
# 6-3、该方法也不会考虑字符串中的空白字符(如空格、制表符、换行符等),因此即使字符串中包含一个空格,isalnum()也会返回False
# 6-4、如果参数为空,isalnum()会返回False
# 7、示例:
s1 = "Hello 123"
print(s1.isalnum())  # 输出: False,因为包含非字母非数字的字符(空格)
s2 = "Hello 123"
print(s2.replace(" ", "").isalnum())  # 输出: True,移除空格后,所有字符都是字母或数字
s3 = "12345"
print(s3.isalnum())  # 输出: True,所有字符都是数字
s4 = "abcdef"
print(s4.isalnum())  # 输出: True,所有字符都是字母
s5 = "Hello World"
print(s5.isalnum())  # 输出: False,包含空格
s6 = "Hello123!"
print(s6.isalnum())  # 输出: False,包含非字母非数字的字符(感叹号)
s7 = ""
print(s7.isalnum())  # 输出: False,空字符串
5-13、isalpha()方法
# 1、方法:str.isalpha
# 2、语法:str.isalpha()
# 3、参数:无
# 4、功能:用于检查字符串中的所有字符是否都是字母,并且至少包含一个字符
# 5、返回值:一个布尔值
# 6、说明:
# 6-1、如果字符串中所有字符都是字母,则返回True,否则返回False
# 6-2、isalpha()方法只考虑字母字符,包括大写和小写字母,它不会考虑数字、空白字符(如空格、制表符、换行符等)或其他特殊字符
# 6-3、如果参数为空,isalpha()会返回False
# 7、示例:
s1 = "Hello"
print(s1.isalpha())  # 输出: True,因为所有字符都是字母
s2 = "Hello123"
print(s2.isalpha())  # 输出: False,因为包含数字
s3 = "Hello World"
print(s3.isalpha())  # 输出: False,因为包含空格
s4 = "Hello!"
print(s4.isalpha())  # 输出: False,因为包含非字母字符(感叹号)
s5 = ""
print(s5.isalpha())  # 输出: False,空字符串
s6 = "12345"
print(s6.isalpha())  # 输出: False,所有字符都是数字
s7 = "Helloもじ"
print(s7.isalpha())  # 输出: True 因为所有字符都是字母,即使字母是用日文表示
5-14、isascii()方法
# 1、方法:str.isdecimal
# 2、语法:str.isdecimal()
# 3、参数:无
# 4、功能:用于检查字符串中的所有字符是否都是十进制数字,且至少有一个这样的字符
# 5、返回值:一个布尔值
# 6、说明:
# 6-1、如果字符串中的所有字符都是十进制数字则返回True,否则返回False
# 6-2、isdecimal()方法只检查字符串中的字符是否属于Unicode十进制数字类别,它不会识别像罗马数字、中文数字或其他非标准的数字表示形式
# 6-3、如果你需要更广泛的数字检测(包括小数点、负号等),你可能需要使用正则表达式或其他字符串处理方法
# 7、示例:
s1 = "12345"
print(s1.isdecimal())  # 输出: True,因为所有字符都是十进制数字
s2 = "12345a"
print(s2.isdecimal())  # 输出: False,因为包含非数字字符
s3 = "123.45"
print(s3.isdecimal())  # 输出: False,因为包含小数点
s4 = ""
print(s4.isdecimal())  # 输出: False,空字符串不包含任何十进制数字字符
s5 = "⅛" #(这里使用的是分数形式的数字)
print(s5.isdecimal())  # 输出: False,因为包含非十进制数字字符
5-15、isdigit()方法
# 1、方法:str.isdigit
# 2、语法:str.isdigit()
# 3、参数:无
# 4、功能:用于检查字符串中的所有字符是否都是数字字符,且至少有一个这样的字符
# 5、返回值:一个布尔值
# 6、说明:
# 6-1、如果字符串中的所有字符都是数字字符则返回 True,否则返回 False
# 6-2、isdigit() 方法只检查字符串中的字符是否属于标准的数字字符(即0-9),它不会识别像罗马数字、中文数字或其他非标准的数字表示形式
# 6-3、如果你需要更广泛的数字检测(包括小数点、负号等),你可能需要使用正则表达式或其他字符串处理方法
# 7、示例:
s1 = "12345"
print(s1.isdigit())  # 输出: True,因为所有字符都是数字字符
s2 = "12345a"
print(s2.isdigit())  # 输出: False,因为包含非数字字符
s3 = "123.45"
print(s3.isdigit())  # 输出: False,因为包含小数点
s4 = ""
print(s4.isdigit())  # 输出: False,空字符串不包含任何数字字符
s5 = "⅛" #(这里使用的是分数形式的数字)
print(s5.isdigit())  # 输出: False,因为包含非数字字符

二、推荐阅读

1、Python-VBA函数之旅-str()函数

2、Python算法之旅

3、Python函数之旅

4、个人主页-神奇夜光杯-CSDN

VPS购买请点击我

文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。

目录[+]