分类 标签 存档 订阅 搜索

Python 字符串方法 | 第一讲(find,rfind,startswith,endswith,islower,isupper,lower,upper,swapcase & title)

473 浏览0 评论

原网址:https://www.geeksforgeeks.org/python-string-methods-set-1-find-rfind-startwith-endwith-islower-isupper-lower-upper-swapcase-title

Python字符串方法 | 第一讲(find,rfind,startswith,endswith,islower,isupper,lower,upper,swapcase & title)

一些字符串的基础已经在下面这些文章里涵盖了:

字符串-第一部分
字符串-第二部分

重要的字符串方法将在这篇文章里讲解。

1. find(“string”, beg, end) 这个函数被用来在字符串中找到子字符串的开始位置。它有三个参数, 子字符串 , 起始索引( 默认为0) and 结束索引(默认为字符串长度).

  • 如果在字符串中没有找到给定的子字符串,那么会返回-1。
  • 它会返回子字符串的开始位置索引

2. rfind(“string”, beg, end) :- 这个函数和find()有几乎相同的功能, 但是它返回子字符串的结束位置索引

# Python code to demonstrate working of  
# find() and rfind() 
str = "geeksforgeeks is for geeks"
str2 = "geeks"
  
# using find() to find first occurrence of str2 
# returns 8 
print ("The first occurrence of str2 is at : ", end="") 
print (str.find( str2, 4) ) 
  
# using rfind() to find last occurrence of str2 
# returns 21 
print ("The last occurrence of str2 is at : ", end="") 
print ( str.rfind( str2, 4) ) 

Output:

The first occurrence of str2 is at : 8
The last occurrence of str2 is at : 21

3. startswith(“string”, beg, end) :- 如果字符串是以指定的子字符串开头的,那么返回True,否则返回False。

4. endswith(“string”, beg, end) :- 如果字符串是以指定的子字符串结尾的,那么返回True,否则返回False。

# Python code to demonstrate working of  
# startswith() and endswith() 
str = "geeks"
str1 = "geeksforgeeksportal"
  
# using startswith() to find if str  
# starts with str1 
if str1.startswith(str): 
        print ("str1 begins with : " + str) 
else : print ("str1 does not begin with : "+ str) 
  
# using endswith() to find  
# if str ends with str1 
if str1.endswith(str): 
    print ("str1 ends with : " + str) 
else : print ("str1 does not end with : " + str) 
) 

Output:

str1 begins with : geeks
str1 does not end with : geeks

5. islower(“string”) :- 如果字符串中所有字符都是 小写的 ,那么返回True,否则返回False。

6. isupper(“string”) :- 如果字符串中所有字符都是因为大写的,那么返回True,否则返回False。

# Python code to demonstrate working of  
# isupper() and islower() 
str = "GeeksforGeeks"
str1 = "geeks"
  
# checking if all characters in str are upper cased 
if str.isupper() : 
       print ("All characters in str are upper cased") 
else : print ("All characters in str are not upper cased") 
  
# checking if all characters in str1 are lower cased 
if str1.islower() : 
       print ("All characters in str1 are lower cased") 
else : print ("All characters in str1 are not lower cased") 

Output:

All characters in str are not upper cased
All characters in str1 are lower cased

7. lower() :- 这个函数返回一个所有字符都 被转化成小写字母的新字符串。

8. upper():- 这个函数返回一个所有字符都被转化成大写字母的新字符串。

9. swapcase() :- 这个函数被用来转换字符串中每一个字符的大小写。

10. title() :- 这个函数把字符串中的每个单词首字母转换为大写,其余则为小写。

# Python code to demonstrate working of  
# upper(), lower(), swapcase() and title() 
str = "GeeksForGeeks is fOr GeeKs"
  
# Coverting string into its lower case 
str1 = str.lower(); 
print (" The lower case converted string is : " + str1) 
  
# Coverting string into its upper case 
str2 = str.upper(); 
print (" The upper case converted string is : " + str2) 
  
# Coverting string into its swapped case 
str3 = str.swapcase(); 
print (" The swap case converted string is : " + str3) 
  
# Coverting string into its title case 
str4 = str.title(); 
print (" The title case converted string is : " + str4) 

Output:

The lower case converted string is : geeksforgeeks is for geeks
 The upper case converted string is : GEEKSFORGEEKS IS FOR GEEKS
 The swap case converted string is : gEEKSfORgEEKS IS FoR gEEkS
 The title case converted string is : Geeksforgeeks Is For Geeks
时间不等人

评论  
留下你的脚步
推荐阅读