Python String Method
Python String Split Method:
In python, the string?split()?method is useful to split the string into a list that contains substrings, and the specified separator separates those.
Following is the pictorial representation of python's string split() method functionality
If you observe the above diagram, we are splitting the string "Suresh-Rohini-Trishika"?with "-" separator using the python split method. Once splitting is done, the split method will return a list of words in the string.
String Split Method Syntax:
Following is the syntax of defining a string split method in python.
string.split(separator, maxsplit)
If you observe the above syntax, the?split()?method accepts two parameters (separator, maxsplit), which are optional.
String Split Method Example:
Following is the example of splitting the given strings with required delimiters using the?split()?method in python.
msg1 =?"Welcome to Tutlane"
msg2 =?"Suresh, Rohini, Trishika"
msg3 =?"Python$Java$C#"
lst1 = msg1.split()
lst2 = msg2.split(", ")
lst3 = msg3.split("$")
print(lst1)
print(lst2)
print(lst3)
If you observe the above example, we are splitting the strings with different delimiters using the?split()?method without specifying the?maxsplit?parameter.
When you execute the above python program, you will get the result as shown below.
['Welcome', 'to', 'Tutlane'] ['Suresh', 'Rohini', 'Trishika'] ['Python', 'Java', 'C#']
In the above example, we didn’t mention any limit (maxsplit) on the number of splits so the?split()?method by default considered?maxsplit?as?-1?and returned the list of all strings.
In case if you want to limit the number of strings split, you need to mention?maxsplit?parameter value in the?split()?method. If?maxsplit?specified, the?split()?method will return a maximum of?maxsplit + 1?items.
Following is the example of splitting the string into a list by using the split() method and limit the number of strings split with maxsplit parameter value.
msg1 =?"Suresh,Rohini,Trishika,Hanshith"
lst1 = msg1.split(",")
lst2 = msg1.split(",",1)
lst3 = msg1.split(",",2)
print(lst1)
print(lst2)
print(lst3)
If you observe the above example, we are limiting the string split by specifying the?maxsplit?parameter value in the?split()?method.
When you execute the above python program, you will get the result as shown below.
['Suresh', 'Rohini', 'Trishika', 'Hanshith']
['Suresh', 'Rohini,Trishika,Hanshith']
['Suresh', 'Rohini', 'Trishika,Hanshith']
This is how you can use the string?split()?method in python to split the string into a list based on your requirements.
Python String Replace Method:
In python, the string?replace()?method is useful to replace the specified substring or character in all occurrences of the given string.
Following is the pictorial representation of string replace() method functionality in python.
If you observe the above diagram, we are replacing the "Hi" word in the given string “Hi Guest Hi” with "Hello" using the python replace method. After completing string replacement, the replace() method will return a new string like "Hello Guest Hello".
String Replace Method Syntax:
Following is the syntax of defining a string replace method in python.
string.replace(old string/char, new string/char, count)?
If you observe the above syntax, the?replace()?method accepts three parameters (old string/char,?new string/char,?count), and the?count?parameter is optional.
String Replace Method Example:
Following is the example of replacing all specified string occurrences using the?replace()?method in python.
msg1 =?"Hi Guest Hi, Learn Python"
rmsg1 = msg1.replace("Hi","Hello")
msg2 =?"aaaacc"
rmsg2 = msg2.replace("a","b")
msg3 =?"1 2 3 4 5 6 7"
rmsg3 = msg3.replace(" ",",")
print(rmsg1)
print(rmsg2)
print(rmsg3)
If you observe the above example, we are replacing the substrings in the given strings with the required substrings/characters using the?replace()?method without specifying the?count?parameter.
When you execute the above python program, you will get the result as shown below.
Hello Guest Hello, Learn Python
bbbbcc
1,2,3,4,5,6,7
In the above example, we didn’t mention any limit (count) on the number of replacements. The?replace()?method replaced all the occurrences of substrings/characters in the given strings.
If you want to limit the number of strings replacement, you need to mention the?count?parameter value in the?replace()?method.
Following is the example of limiting the number of substrings replacement with?count?parameter value using?replace()?method.
msg1 =?"Hi Guest Hi"
rmsg1 = msg1.replace("Hi","Hello")
rmsg2 = msg1.replace("Hi","Hello",0)
rmsg3 = msg1.replace("Hi","Hello",1)
print(rmsg1)
print(rmsg2)
print(rmsg3)
If you observe the above example, we are limiting the string replacement by specifying the?count?parameter value in?replace()?method.
When you execute the above python program, you will get the result as shown below.
Hello Guest Hello
Hi Guest Hi
Hello Guest Hi
This is how you can use a string?replace()?method in python to replace all substring occurrences in the given string based on your requirements.?
Python String Find Method:
In python, the string find() method is useful to search the string for specified substring and return the index position of the first occurrence of the specified substring. In case if the specified substring/character is not found, it will return?-1.
Following is the pictorial representation of a string?find()?method functionality in python.
If you observe the above diagram, we are trying to find the "Hi" word in the given string “Hi Guest Hi” using the?find?function in python and it returned the index position of the first occurred specified substring as?0. In python, the index position will always start from?0, so it returned the index position?0.
String Find Method Syntax:
Following is the syntax of defining a string find method in python.
string.find(string/char, startIndex, endIndex)
If you observe the above syntax, the?find()?method accepts three parameters (string/char,?startIndex,?endIndex). Here,?startIndex?and?endIndex?parameters are optional.
String Find Method Example:
Following is the example of searching the specified substring/character in the given string using the string find() method in python.
msg =?"Hi Guest hi, welcome to tutlane"
rmsg1 = msg.find("hi")
rmsg2 = msg.find("Tutlane")
rmsg3 = msg.find("to")
rmsg4 = msg.find("Hi")
print("Substring 'hi':", rmsg1)
print("Substring 'Tutlane':", rmsg2)
print("Substring 'to':", rmsg3)
print("Substring 'Hi':", rmsg4)
If you observe the above example, we are searching the required substrings in the given string using?find()?method without specifying?start/end?index parameters.
When you execute the above python program, you will get the result as shown below.
Substring 'hi': 9
Substring 'Tutlane': -1
Substring 'to': 21
Substring 'Hi': 0
Python String Index Method:
In python, the string index() method is useful to search the string for specified substring and return the index position of the first occurrence of the specified substring. In case if the specified substring/character is not found, it will raise an exception.
Following is the pictorial representation of string?index()?method functionality in python.
If you observe the above diagram, we are trying to find the "Hi" word in the given string “Hi Guest Hi” using the?index?function in python, and it returned the index position of the first occurred specified substring as?0. In python, the index position will always start from?0,?so it returned the index position?0.
In python, the string?index()?method is same as the?string find() method, but the only difference is the?string find() method?will return?-1?in case if the specified substring/character is not found, but the?index()?method will raise an exception.
String Index Method Syntax:
Following is the syntax of defining a string index method in python.
string.index(string/char, startIndex, endIndex)
If you observe the above syntax, the?index()?method accepts three parameters (string/char,?startIndex,?endIndex). Here,?startIndex?and?endIndex?parameters are optional.
String Index Method Example:
Following is the example of searching the specified substring/character in the given string using the index() method in python.
msg =?"Hi Guest hi, welcome to tutlane"
rmsg1 = msg.index("hi")
print("Substring 'hi':", rmsg1)
rmsg2 = msg.index("Python")
print("Substring 'Python':", rmsg2)
If you observe the above example, we are searching the required substrings in the given string using?index()?method without specifying?start/end?index parameters.
When you execute the above python program, you will get the result as shown below.
Substring 'hi': 9 Traceback (most recent call last): ?
File "D:\Python Examples\pythonmethods.py", line 4, in <module> ???
rmsg2 = msg.index("Python")
ValueError: substring not found
Python String Format Method:
In python, the string format() method is useful to format complex strings and numbers. The format strings will contain the curly braces?{ }?and the?format()?method will use those curly braces?{ }?as placeholders to replace with the content of the parameters.
Following is the pictorial representation of string?format()?method functionality in python.
If you observe the above diagram, the format string (s) contains placeholders ({ }), and the python?format?method inserted the required text in the place of placeholders and returned the formatted string.
String Format Method Syntax:
Following is the syntax of defining a string format method in python.
string.format(arg1, arg2, etc.)
The python format() method will accept unlimited arguments and replace the curly brace?{ }?placeholders with the content of the respective arguments.
String Format Method Example:
Following is the example of formatting the strings using the?format()?method in python.
str1 =?"{} to {}".format("Welcome",?"Tutlane. com")
print(str1)
name =?"Suresh"
age =?33
str2 =?"My name is {}, and I am {} years old"
print(str2.format(name, age))
If you observe the above example, we created strings with a curly brace?{ }?placeholders and we are replacing the placeholders with required argument values using?format()?method.
When you execute the above python program, you will get the result as shown below.
Welcome to Tutlane. com
My name is Suresh, and I am 33 years old
If you observe the above example, we created strings with placeholders without specifying any order, and we are not sure whether the arguments are placed in the correct order or not.
Python String Lower (Lowercase) Method:
In python, the string lower() method is useful to convert all the string characters to lowercase. Generally, the lower() method will convert all the uppercase letters in a string to lowercase. In case, if no uppercase letters in a string, it will return the original string.
Following is the pictorial representation of the string?lower()?method functionality in python.
If you observe the above diagram, we are trying to convert the given string “Hi GUEST Hi” to lowercase using the?lower?function in python. The?lower()?method has returned the string by converting all the string characters to lowercase.
String Lower Method Syntax:
Following is the syntax of defining a string lower method in python.
string.lower()
The python?lower()?method will not accept any parameters.
String Lower Method Example:
Following is the example of converting the given string characters to lowercase using the string?lower()?method in python.
msg1 =?"Hi GUEST Hi"
print(msg1.lower())
msg2 =?"WELCOME TO TUTLANE"
print(msg2.lower())
msg3 =?"learn python"
print(msg3.lower())
If you observe the above example, we are converting the given string characters to lowercase using the?lower()?method.
When you execute the above python program, you will get the result as shown below.
hi guest hi
welcome to tutlane
learn python
This is how you can use the string?lower()?method in python to convert all the characters of the given string to lowercase based on your requirements.?
Python String Upper (Uppercase) Method:
In python, the string upper() method is useful to convert all the string characters to uppercase. Generally, the upper() method will convert all the lowercase letters in a string to uppercase. In case, if no lowercase letters in a string, it will return the original string.
Following is the pictorial representation of the string?upper()?method functionality in python.
If you observe the above diagram, we are trying to convert the given string “Hi guest Hi” to uppercase using the?upper?function in python. The?upper()?method has returned the string by converting all the string characters to uppercase.
String Upper Method Syntax:
Following is the syntax of defining a string upper method in python.
string.upper()
The python upper() method will not accept any parameters.
String Upper Method Example:
Following is the example of converting the given string characters to uppercase using the string?upper()?method in python.
msg1 =?"Hi guest Hi"
print(msg1.upper())
msg2 =?"welcome to tutlane"
print(msg2.upper())
msg3 =?"LEARN PYTHON"
print(msg3.upper())
If you observe the above example, we are converting the given string characters to uppercase using the?upper()?method.
When you execute the above python program, you will get the result as shown below.
HI GUEST HI
WELCOME TO TUTLANE
LEARN PYTHON
This is how you can use the string upper() method in python to convert all the given string characters to uppercase based on your requirements.?
Python String Swapcase Method:
In python, the string swapcase() method is useful to convert the given string lowercase characters to uppercase and uppercase characters to lowercase.
Following is the pictorial representation of the string?swapcase()?method functionality in python.
If you observe the above diagram, we are trying to convert the given string “Hi GUEST Hi” lowercase characters to uppercase and uppercase characters to lowercase using the?swapcase?function in python.
String Swapcase Method Syntax:
Following is the syntax of defining a string swapcase method in python.
string.swapcase()
The python swapcase() method will not accept any parameters.
String Swapcase Method Example:
Following is the example of converting the given string lowercase characters to uppercase and uppercase characters to lowercase using the string?swapcase()?method in python.
msg1 =?"Hi guest Hi"
print(msg1.swapcase())
msg2 =?"welcome to tutlane"
print(msg2.swapcase())
msg3 =?"LEARN PYTHON"
print(msg3.swapcase())
If you observe the above example, we are converting the given string lowercase characters to uppercase and uppercase characters to lowercase using the?swapcase()?method.
When you execute the above python program, you will get the result as shown below.
hI GUEST hI
WELCOME TO TUTLANE
learn python
This is how you can use the string swapcase() method in python to swap the given string characters based on your requirements.?
Python String Title Method:
In python, the string title() method is useful to convert the first letter of every word in the given string to uppercase. If the first letter of the word is a number or symbol, it will convert the next letter to uppercase.
Following is the pictorial representation of the string?title()?method functionality in python.
If you observe the above diagram, we are trying to convert the given string “hi guest hi” to title case using the?title?function in python.
String Title Method Syntax:
Following is the syntax of defining a string title method in python.
string.title()
The python?title()?method will not accept any parameters.
String Title Method Example:
Following is the example of converting the first letter of every word in the given string to?uppercase?using the string?title()?method in python.
msg1 =?"hi guest hi"
print(msg1.title())
msg2 =?"welcome to 2tutlane"
print(msg2.title())
msg3 =?"3#learn python"
print(msg3.title())
If you observe the above example, we are converting the first letter of every word in the given string to uppercase using the?title()?method.
When you execute the above python program, you will get the result as shown below.
Hi Guest Hi
Welcome To 2Tutlane
3#Learn Python
This is how you can use the string title() method in python to convert the given strings to title case based on your requirements.
Python String Capitalize Method:
In python, the string capitalize() method is useful to convert the first letter of the given string to uppercase and remaining all characters to lowercase.
Following is the pictorial representation of string?capitalize()?method functionality in python.
If you observe the above diagram, we are trying to capitalize the given string “HI Guest HI” using the?capitalize?function in python.
String Capitalize Method Syntax:
Following is the syntax of defining a string capitalize method in python.
string.capitalize()
The python capitalize method will not accept any parameters.
String Capitalize Method Example:
Following is the example of converting the first letter of the given string to uppercase and remaining all characters to lowercase using the string?capitalize()?method in python.
msg1 =?"HI Guest HI"
print(msg1.capitalize())
msg2 =?"welcome to tutlane"
print(msg2.capitalize())
msg3 =?"2LEARN PYTHON"
print(msg3.capitalize())
If you observe the above example, we are converting the first letter of a given string to uppercase using the?capitalize()?method.
When you execute the above python program, you will get the result as shown below.
Hi guest hi
Welcome to tutlane
2learn python
This is how you can use the string?capitalize()?method in python to convert the first letter of given strings to uppercase based on your requirements.?
Slicing Strings:
You can return a range of characters by using the slice syntax.
Specify the start index and the end index, separated by a colon, to return a part of the string.
b =?"Hello, World!"
print(b[2:5])
?