📜  Python字符串方法

📅  最后修改于: 2022-05-13 01:55:23.335000             🧑  作者: Mango

Python字符串方法

Python字符串是用引号括起来的 Unicode字符序列。在本文中,我们将讨论内置函数,即Python提供的对字符串进行操作的函数。

注意:每个字符串方法都不会更改原始字符串,而是返回具有更改属性的新字符串。

字符串的大小写更改

以下函数用于更改字符串。

  • lower():将字符串的所有大写字符转换为小写
  • upper():将字符串的所有小写字符转换为大写
  • title():将字符串转换为标题大小写

示例:更改Python字符串的大小写。

Python3
# Python3 program to show the
# working of upper() function
text = 'geeKs For geEkS'
  
# upper() function to convert
# string to upper_case
print("\nConverted String:")
print(text.upper())
  
# lower() function to convert
# string to upper_case
print("\nConverted String:")
print(text.lower())
  
# rpartition breaks the string
print("\nConverted String:")
print(text.title())
  
# original string never changes
print("\nOriginal String")
print(text)


输出

Converted String:
GEEKS FOR GEEKS

Converted String:
geeks for geeks

Converted String:
Geeks For Geeks

Original String
geeKs For geEkS

Python字符串方法表

Function Name Description
capitalize()Converts the first character of the string to a capital (uppercase) letter
casefold()Implements caseless string matching
center()Pad the string with the specified character.
count()Returns the number of occurrences of a substring in the string.
encode()Encodes strings with the specified encoded scheme
endswith()Returns “True” if a string ends with the given suffix
expandtabs()Specifies the amount of space to be substituted with the “\t” symbol in the string
find()Returns the lowest index of the substring if it is found
format()Formats the string for printing it to console
format_map()Formats specified values in a string using a dictionary
index()Returns the position of the first occurrence of a substring in a string
isalnum()Checks whether all the characters in a given string is alphanumeric or not
isalpha()Returns “True” if all characters in the string are alphabets
isdecimal()Returns true if all characters in a string are decimal
isdigit()Returns “True” if all characters in the string are digits
isidentifier()Check whether a string is a valid identifier or not
islower()Checks if all characters in the string are lowercase
isnumeric()Returns “True” if all characters in the string are numeric characters
isprintable()Returns “True” if all characters in the string are printable or the string is empty
isspace()Returns “True” if all characters in the string are whitespace characters
istitle()Returns “True” if the string is a title cased string
isupper()Checks if all characters in the string are uppercase
join()Returns a concatenated String
ljust()Left aligns the string according to the width specified
lower()Converts all uppercase characters in a string into lowercase
lstrip()Returns the string with leading characters removed
maketrans() Returns a translation table
partition()Splits the string at the first occurrence of the separator 
replace()Replaces all occurrences of a substring with another substring
rfind()Returns the highest index of the substring
rindex()Returns the highest index of the substring inside the string
rjust()Right aligns the string according to the width specified
rpartition()Split the given string into three parts
rsplit()Split the string from the right by the specified separator
rstrip()Removes trailing characters
splitlines()Split the lines at line boundaries
startswith()Returns “True” if a string starts with the given prefix
strip()Returns the string with both leading and trailing characters
swapcase()Converts all uppercase characters to lowercase and vice versa
title()Convert string to title case
translate()Modify string according to given translation mappings
upper()Converts all lowercase characters in a string into uppercase
zfill()Returns a copy of the string with ‘0’ characters padded to the left side of the string

注意:有关Python字符串的更多信息,请参阅Python字符串教程。