📜  Python字符串方法 |设置 1(查找、rfind、startwith、endwith、islower、isupper、lower、upper、swapcase 和标题)

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

Python字符串方法 |设置 1(查找、rfind、startwith、endwith、islower、isupper、lower、upper、swapcase 和标题)

以下文章中介绍了一些字符串基础知识
弦乐第 1 部分
弦乐 Part-2
本文将讨论重要的字符串方法
1. find(“字符串”, beg, end) :- 该函数用于查找子字符串在字符串中的位置。它有 3 个参数,子字符串、起始索引(默认为 0)和结束索引(默认为字符串长度)

  • 如果在给定范围内未找到字符串,则返回“-1”。
  • 如果找到,则返回第一次出现的字符串。

2. rfind(“字符串”, beg, end) :- 这个函数和 find() 的工作方式类似,但是它返回字符串的最后一次出现的位置。

Python
# 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) )


Python3
# 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)


Python3
# 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")


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


输出:

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

3. startswith(“字符串 ”, beg, end) :- 如果函数以提到的字符串(前缀)开头,则该函数的目的是返回 true,否则返回 false。
4. endswith(“ 字符串”, beg, end) :- 这个函数的目的是如果函数以提到的字符串(后缀)结尾则返回true,否则返回false。

Python3

# 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)

输出:

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

5. islower(“字符串 ”) :- 如果字符串中的所有字母都是小写,则此函数返回 true,否则返回 false。
6. isupper(“ 字符串 ”) :- 如果字符串中的所有字母都大写,则此函数返回 true,否则返回 false。

Python3

# 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")

输出:

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

7. lower() :- 此函数返回所有字母都转换为小写的新字符串。
8. upper() :- 此函数返回所有字母都转换为大写的新字符串。
9. swapcase() :- 该函数用于交换字符串的大小写,即大写转换为小写,反之亦然。
10. title() :- 该函数将字符串转换为其标题大小写,即字符串中每个单词的首字母大写,否则全部小写。

Python3

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

输出:

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