Python字符串方法 |设置 3 (strip, lstrip, rstrip, min, max, maketrans, translate, replace & expandtabs())
下面的集合中介绍了一些字符串方法。
字符串方法第 1 部分
字符串方法第 2 部分
本文讨论了更多方法
1. strip() :- 该方法用于删除其参数中提到的所有前导和尾随字符。
2. lstrip() :- 该方法用于删除其参数中提到的所有前导字符。
3. rstrip() :- 此方法用于删除其参数中提到的所有尾随字符。
Python
# Python code to demonstrate working of
# strip(), lstrip() and rstrip()
str = "---geeksforgeeks---"
# using strip() to delete all '-'
print ( " String after stripping all '-' is : ", end="")
print ( str.strip('-') )
# using lstrip() to delete all trailing '-'
print ( " String after stripping all leading '-' is : ", end="")
print ( str.lstrip('-') )
# using rstrip() to delete all leading '-'
print ( " String after stripping all trailing '-' is : ", end="")
print ( str.rstrip('-') )
Python
# Python code to demonstrate working of
# min() and max()
str = "geeksforgeeks"
# using min() to print the smallest character
# prints 'e'
print ("The minimum value character is : " + min(str))
# using max() to print the largest character
# prints 's'
print ("The maximum value character is : " + max(str))
Python
# Python code to demonstrate working of
# maketrans() and translate()
from string import maketrans # for maketrans()
str = "geeksforgeeks"
str1 = "gfo"
str2 = "abc"
# using maketrans() to map elements of str2 with str1
mapped = maketrans( str1, str2 )
# using translate() to translate using the mapping
print "The string after translation using mapped elements is : "
print str.translate(mapped)
Python
# Python code to demonstrate working of
# replace()
str = "nerdsfornerds is for nerds"
str1 = "nerds"
str2 = "geeks"
# using replace() to replace str2 with str1 in str
# only changes 2 occurrences
print ("The string after replacing strings is : ", end="")
print (str.replace( str1, str2, 2))
Python3
# Python code to illustrate expandtabs()
string = 'GEEKS\tFOR\tGEEKS'
# No parameters, by default size is 8
print (string.expandtabs())
# tab size taken as 2
print(string.expandtabs(2))
# tab size taken as 5
print(string.expandtabs(5))
输出:
String after stripping all '-' is : geeksforgeeks
String after stripping all leading '-' is : geeksforgeeks---
String after stripping all trailing '-' is : ---geeksforgeeks
4. min(“字符串”) :- 此函数返回字符串中的最小值字母表。
5. max(“字符串”) :- 这个函数从字符串中返回最大值字母。
Python
# Python code to demonstrate working of
# min() and max()
str = "geeksforgeeks"
# using min() to print the smallest character
# prints 'e'
print ("The minimum value character is : " + min(str))
# using max() to print the largest character
# prints 's'
print ("The maximum value character is : " + max(str))
输出:
The minimum value character is : e
The maximum value character is : s
6. maketrans() :- 用于将字符串1 的内容与字符串2 的内容进行映射,并在稍后使用 translate() 翻译相应的索引。
7. translate() :- 这用于交换借助 maketrans() 映射的字符串元素。
Python
# Python code to demonstrate working of
# maketrans() and translate()
from string import maketrans # for maketrans()
str = "geeksforgeeks"
str1 = "gfo"
str2 = "abc"
# using maketrans() to map elements of str2 with str1
mapped = maketrans( str1, str2 )
# using translate() to translate using the mapping
print "The string after translation using mapped elements is : "
print str.translate(mapped)
输出:
The string after translation using mapped elements is :
aeeksbcraeeks
在上面的代码中,使用 translate函数将字符串中的“g”替换为 a,将“f”替换为 b,将字符串中的“o”替换为“c”。
8.replace() :- 该函数用于用字符串中的新子字符串替换子字符串。这个函数有 3 个参数。要替换的字符串,将替换的新字符串和表示替换操作限制的最大值(默认为无限制)。
Python
# Python code to demonstrate working of
# replace()
str = "nerdsfornerds is for nerds"
str1 = "nerds"
str2 = "geeks"
# using replace() to replace str2 with str1 in str
# only changes 2 occurrences
print ("The string after replacing strings is : ", end="")
print (str.replace( str1, str2, 2))
输出:
The string after replacing strings is : geeksforgeeks is for nerds
该方法由 Chinmoy Lenka 提供
9. expandtabs() :- 它用于使用给定的选项卡大小将所有制表字符(“\t”)替换为空格或简单的空格,这是可选的。
语法:字符串.tabsize(tabsize)
参数:指定一个制表字符要替换的字符数。默认情况下,该函数将选项卡大小设为 8。
返回值:一个字符串,其中所有的制表字符都替换为空格。
Python3
# Python code to illustrate expandtabs()
string = 'GEEKS\tFOR\tGEEKS'
# No parameters, by default size is 8
print (string.expandtabs())
# tab size taken as 2
print(string.expandtabs(2))
# tab size taken as 5
print(string.expandtabs(5))
输出:
GEEKS FOR GEEKS
GEEKS FOR GEEKS
GEEKS FOR GEEKS