Strings are a sequence of characters which can be stored either as a constant or a different variable. Strings are considered as a datatype. Typically, programmers must enclose strings in quotation marks for the data to recognized as a string and not a number or variable name. Shown below are some of the most used string methods on a daily basis and are one of the most commonly asked interview questions.
# Declaring a string variable
string = "This is a python tutorial"
print(string)
This is a python tutorial
print(type(string))
<class 'str'>
In Python strings, the backslash " " is a special character, also called the "escape" character. It is used in representing certain whitespace characters: "\t" is a tab, "\n" is a new line, and "\r" is a carriage return. Finally, " " can be used to escape itself: "" is the literal backslash character.
# THis is an escape sequence.
string = "This is a \"Google Colab\" python notebook"
print(string)
This is a "Google Colab" python notebook
Strings can be accessed by their index in order to get the value. To do this all you have to do is just place the number (index value) inside the pair of square brackets along with the name of the string.
string = "Python"
print(string[2])
t
print(string[5])
n
Slicing a string helps to get a set of characters from a string. This is really helpful when we want to access a particular set of characters in a string. Below are some slicing variants that are useful.
string = "programming"
string
'programming'
Getting one character of the string
print(string[0:1])
p
Getting the first three characters from the string
print(string[0:3])
pro
Getting the first three characters from the string (Alternate)
print(string[:3])
pro
Getting the last three characters from the string
print(string[-3:])
ing
Getting all characters but excluding three first characters from the string
print(string[3:])
gramming
Getting all characters but excluding the last three characters from the string
print(string[:-3])
programm
Reversing all the characters in a given string
print(string[::-1])
gnimmargorp
Alternative to printing all characters in a string
print(string[::])
programming
4. Splitting a string
Sometimes splitting a string is a handy option because it is one of the easiest ways to convert a string into a list. I know I have not spoken about the list but keep in mind that split converts a string into a list. You can find the material on Python Lists that is written by me which can provide enough idea to master python lists Python Lists
String = "Computer Programming"
String
'Computer Programming'
type(String)
str
list = String.split()
list
['Computer', 'Programming']
type(list)
list
The Replace function in python is one of the best function that can be applied to strings. For example, shown below is a string "Money" we need to replace the dollar and the comma sign and this can be done as shown below.
Money = '$113,678'
print(Money)
print("===========================")
print(type(Money))
$113,678 =========================== <class 'str'>
Money = Money.replace('$', '')
Money
'113,678'
Money = Money.replace(',', '')
Money
'113678'
Money = int(Money)
print(Money)
print("===========================")
print(type(Money))
113678 =========================== <class 'int'>
The join function is python is used to join the string according to the specified pattern.
String = "Python Programming is fun"
String
'Python Programming is fun'
String = " ".join(String)
String
'P y t h o n P r o g r a m m i n g i s f u n'
The capitalize function capitalizes the first character in the word or a string.
string = "programming"
string
'programming'
string = string.capitalize()
string
'Programming'
The center method returns a string which is padded with the specified character.
string = "python"
string
'python'
print(string.center(15, '*'))
*****python****
The find method returns the index of the given substring position. If the value is not found it returns, -1.
string = "programming"
string
'programming'
print(string.find('p'))
0
print(string.find('t'))
-1
The strip function strips or removes the white spaces both from the starting and the ending of a string.
string = " programming is easy "
string
' programming is easy '
print(string.strip())
programming is easy
Hence above are the very important techniques or functions of Strings in Python. Some of the examples were referred from Python Strings. I have written this tutorial in a simple way such that everybody can understand and master the concepts of Strings in Python without prior programming knowledge or experience.