The string is the collection of characters. The strings in python are “immutable”; we cannot change a string once they are created. In python, whatever data we take as input will be read as a string. In python, string concatenation is possible.
Indexing a String
The indexing of a string in python starts with zero and counts until the last character. For accessing an element in a string, we use the method of indexing.
my_string= “…Data…” my_string[n] #where n is an index of the element in the string.
my_string= “Python” #To access the first element in the string print(my_string) #To access the second element in the string print(my_string)
Slicing a String
In python, a string can be sliced into many parts by the indexing method. The slicing method does not affect the original string but creates a string with given indexing values.
my_string= “…Data….” print( my_string[n:m] ) #where n and m are any two integers
my_string= “Python” #Starts 2ndindex to 4th index. print(my_string[2:5]) #Starts 0th index to 3rd index. print(my_string[0:4]) #Starts 0th index to 4th index. Print(my_string[:5])
We can also use the negative indexes of the elements in the string to perform the slicing operation.
my_string= “String” #printing the last but one two elements of the string print(my_string[-3:-1]) #printing the first two elements of the string Print(my_string[-6:-4])
We can also reverse a string using the negative indexing method through the slicing operation.
my_string= “String” print(my_string[::-1])
Length of a String
The Length of the string is generally calculated using the “ len( ) ” function. The index of the last element can be calculated by using the Length of the string.
index of last element= Length of the string – 1
my_string= “String” #printing the Length of string print(len(my_string))
Deletion of an Element from the String
The strings are generally immutable, so we cannot change the given string. Hence we need to assign the updated string to a new variable. Generally, we use two functions to perform this operation they are,
- Using replace( )
- Using translate( )
Using replace( ):
The replace function removes a character from the string, replaces it with the given character, and updates the given string. The replace ( ) function replaces the previous character with the new character using the double quotes ( " " ).
string.replace( previous character, new character, count)
String: It is a positional argument consisting of the string.
Previous character: The character who is to be replaced.
New character: The character which is given to update the string
Count: This is an optional character which tells us how many times the given character needs to be repeated.
my_string= “ replace “ updated_string=string.replace( “l” , “u”) #display the updated version of the string print(updated_string)
The major drawback of this method is that it replaces all the given characters with the new character, that is, if a character "j" has occurred two times in the 3rd and 6th positions of a string.
If we want to replace the character with the new character "o" only at the 3rd position, we can't perform this operation because the replace function replaces both the "j" characters; this is the major drawback of this operation.
Using translate( )
The translate( ) function updates the string by removing the character. The translate( ) removes characters based on the table value.
string= “Time” a = “i” b= “o” table =string.maketrans(a, b) print(stringtransulate(table))
Removing the first character from the string:
The first character in a string can be removed by using the following methods:
- Removing the first character using the slicing technique.
- Removing first character using the str.lstrip( )function.
Using slicing technique:
The first character in a string can be removed using the slicing technique-str[1:]. This str[1:] print the whole string except the first character.
s = “slice” updated_string= s[1:] print (updated_string)
Using str.lstrip( ) Method
The str.lstrip( ) method removes the first character from the string, and if the first character is repeated more than once, it will remove all the first character, which is repeated and prints the remaining string.
s = “SString” updated_string = string.strip(“S”) print (updated_string)