By reading this article your following problems will be answered.
How to use string functions in Python.
How to use ‘find’ function in Python.
How to extract characters in Python.
How to use substrings in Python.
How to replace text in Python.
String functions in Python
- We can use the ‘find’ function to locate letters or words in a string.
- This gives out the the number which describes the starting position or the first character (including spaces) of the string we are searching for is found in the string which we are searching in.
- Python start counting the position starting from 0. That means the first letter of any string is the 0 position. (E.g.: chocolate – 012345678 describes the positions of each letters.)
- The following code shows this.
1. word_1 = “chocolate”
2. word_2 = “choco”
3. word_3 = “late”
4. word_1.find(word_2)
5. word_1.find(word_3)
- The output of line 4 is ‘0’ and output of line 5 is ‘5’.
- If the string we are searching for is not available inside the string we are searching in, it gives an output of -1 (Negative one). (Which is the default value.)
- Also the ‘find’ is CASE SENSITIVE.
- It we take a long string of text, the ‘find‘ command will only find the FIRST INSTANCE which the string we are searching for is available in the string we are searching in.
Extracting characters in Python
- To extract a character inside a string, we can do the following.
word = “Airline”
char = word[2]
print(char)
- The output of ‘print(char)’ will be ‘r’.
- To extract the character we need, we can use square brackets following the variable.
- So the it will give only the relevant character as the output.
- Also note that Python will always starts counting anything starting from 0, so the first character of any string is will definitely be zero.
Substrings in Python
- To extract an entire section of a string we can use substrings.
- We can do this by providing the starting and the ending character of a string. By doing this, it will output the entire section of characters which is in the middle of the starting and the ending character.
- The starting character is the character that we need to start extract from.
- But the ending character is the character which is after the character which is the last character we need to extract.
- So as an example, if we need to extract ‘cat’ from the string ‘Education’, the starting character will be 3, and the ending character will be 6 and not 5. Even though our last character is at the position 5 we need to give the number of the character which is situated after the last character we need to extract.
- The following example shows how to extract ‘cat’ from ‘education’.
word = “Education”
slice = word[3:6]
print(slice)
- The output of ‘print(slice)’ will be ‘cat’.
- So we need to give the starting character and the ending character with the colon in the middle within square brackets to extract the relevant part of the string.
- If our anticipated last character is actually the last character of our string and, if there are no more characters available after our anticipated last character, we should always make sure to +1 to the position of our lat character.
- Generally, we should always add 1 (+1) to our anticipated last character.
- If we copy a part of another string to another variable, it is known as slicing.
- Also to get every character after our desired character, we can keep the value of the last character as a blank as following, so it will give all the characters starting from our desired character
word = “gentleman”
slice = word[6:]
print(slice)
- The output of the line ‘print(slice)’ will be ‘man’.
- Also we can extract every character before our ending character by leaving the starting character as a blank.
Replacing text in Python
- The ‘replace’ function is used to replace specified text with another text in Python.
- We can use the ‘replace’ function to replace certain specified text with another text which are stored in variables.
- The following example shows how to replace a specified word with another word in a sentence.
sentence_1 = “I love you.”
sentence_2 = sentence_1.replace(“love”, “hate”)
print(sentence_2)
- The output of the line ‘print(sentence_2)’ will be ‘I hate you’.
- In the above example, the word ‘love’ will be replaced by the word ‘hate’.
- Inside the bracket in the ‘replace’ function, first we need to mention the word we are going to replace (The old word), and followed by a comma, we need to mention the word we going to replace with (The new word.)
- Or those words can be variables in the above specified order.
- If the word we are going to replace is not available in the original text, then it will keep the original text untouched. That means nothing will happen to that.
Know more about Python