Fun with Fantastic String Data Type in Python
Sahil
Posted on August 16, 2022
As we have seen in the previous blog, the string is a sequence of characters. We can store any kind of data in this data type. For example, name, address, URL, tokens or any other type of data.
1. Reverse the Verse using Slice Operator
In this exercise, we will see different ways to reverse the string in python using the slice operator. There are many other ways to reverse the string. But my main goal is to show you how to use the slice operator.
Slice Operator
The slice operator is written using square brackets and three arguments. These three arguments are start, stop and step. Start suggests where to start the operation; stop suggests, where to stop the operations and step suggests the number of steps (1, 2, -3, -1, etc.) to take. The slice operator can be used with strings, lists and tuples. We can not use it int, boolean set or dictionary.
Note: We can ignore the step argument. It is an optional argument. In some cases, we can neglect the use of start and even stop arguments as well.
string[start:stop:step] or list[start:stop:step] or tuple[start:stop:step]
Let's assume that we have a string "Python Programming". In every programming language, the index starts from 0. So, for our string, we have the indexes from 0 up to 17.
If we want data from index 2 to 12, we have to write str1[2:13]. The slice operator considers the stopping point as 12. Or in general, one point before the given stopping point.
str1 = "Python Programming"
print(str1[2:13]) #OUTPUT : thon Progra
Now, you will say I want every 3rd data from index 2 up to index 13. So, our slice operator will look something like str1[2:13:3]. From the image below, you can observe that it took the start index and then every 3rd index. So, we got the following indexes 2, 5, 8 and 11. As the stopping point was 13, the compiler did not consider the 14th index.
str1 = "Python Programming"
print(str1[2:13:3]) #OUTPUT : tnrr
As I mentioned earlier we can ignore the start and stop arguments in the slice operator. So, in some cases when we want starting point as 0 we can neglect starting argument. For example, we want str1[0:13:3] then we can write it as str1[:13:3].
str1 = "Python Programming"
print(str1[:13:3]) #OUTPUT : Ph oa
Same way, we can ignore the stopping point as well. So, you can either write str1[10::3] or str1[10:18:3]
str1 = "Python Programming"
print(str1[10::3]) #OUTPUT : gmn
We can ignore both stop and start endpoints at the same time as well. You can write something like str1[::3]. We should neglect using start and stop arguments in those cases where we have step arguments available. Otherwise, there is no meaning in using just str1[:] or str1[::]. It will return the exact string.
str1 = "Python Programming"
print(str1[::3]) #OUTPUT : Ph oai
print(str1[::]) #OUTPUT : Python Programming
print(str1[:]) #OUTPUT : Python Programming
Did I tell you that we can use negative start, stop and step arguments as well 🤔?
We can use negative arguments. But, be careful and test it properly before using a negative start, stop or step argument. By default, the step argument is 1.
In the example given below, as we have not given a step argument it will take 1 as the default value. Now, -1 is the last element of our string so after adding 1 into it we get 0. By, python will not go at the beginning. It will try to find index 0 after index -1. So, we get an empty string as output.
str2 = "Python Programming"
print(str2[-1:-5]) #OUTPUT :
Now, if we start slice operation from -5 instead of -1 then we will get the following output. The reason for this is, that when the compiler tries to add 1 into -5 it gets -4 and it exists after the index -5.
str2 = "Python Programming"
print(str2[-5:-1]) #OUTPUT : mmin
str2 = "Python Programming"
print(str2[-5:-1]) #OUTPUT : nmagr
str2 = "Python Programming"
print(str2[-1:-5:-1]) #OUTPUT : gnim
So 😮💨, how to use the slice operator to reverse the string. I know, by now you might have an idea how to do it. Still, let's do it!
str1 = "Python Programming"
str2 = str1[::-1]
print(str2) #OUTPUT : gnimmargorP nohtyP
2. Manipulate the String using 7 popular methods
There are many inbuilt methods provided by python for string manipulation. Here, I will go through some of them that you might use often.
- capitalize(), title() & count()
- find() & index()
- lower() & upper()
- islower() & isupper()
- strip(), lstrip() & rstrip()
- isalpha(), isnumeric() & isalnum()
- split(), join() & replace()
capitalize(), title() & count()
capitalize() method converts the first character of the string to a Capital Letter. Whereas, the title() method capitalizes the first character of every word in a string.
str1 = "learn python programming"
print(str1.capitalize()) #OUTPUT: Learn python programming
print(str1.title()) #OUTPUT: Learn Python Programming
count() method helps to count the occurrence of a specific character(s) or substring in a string. White space, numbers and symbols all are considered as characters. You can use a string variable as well to use the count method.
str1 = "learn python programming learn"
str2 = "python"
print(str1.count('learn')) #OUTPUT: 2
print(str1.co4unt(' ')) #OUTPUT: 3
print(str1.count(str2)) #OUTPUT: 1
find() & index()
Searches the string for a specified character(s) or sub-string and returns the position of where it was found. The main difference between find() and index() is when specified character(s) or sub-string does not exist find() method returns -1 whereas, index() method throws an error.
find()
str1 = "learn python programming learn"
print(str1.find('learn')) #OUTPUT: 0
print(str1.find(' ')) #OUTPUT: 5
print(str1.find('z')) #OUTPUT: -1
index()
str1 = "learn python programming learn"
print(str1.index('learn')) #OUTPUT: 0
print(str1.index(' ')) #OUTPUT: 5
print(str1.index('z'))
"""
OUTPUT
---
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found
"""
Note: Triple quotes has two purposes. You can use them for multi-line comments or assignment of multi-line string data.
My recommendation would be to use the find() method instead of index().
lower() & upper()
Sometimes, we need to convert data into either lower case or upper case. Most probably in data science and machine learning tasks we need to convert everything into lowercase.
lower()
str1 = "Learn Python Programming from @Sahil Fruitwala on 13-Oct 🙌"
print(str1.lower())
#OUTPUT: learn python programming from @sahil fruitwala on 13-oct 🙌
upper()
str1 = "Learn Python Programming from @Sahil Fruitwala on 13-Oct 🙌"
print(str1.upper())
#OUTPUT: LEARN PYTHON PROGRAMMING FROM @SAHIL FRUITWALA ON 13-OCT 🙌
Note: Any character except alphabets will be ignored when we apply the upper() or lower() method.
islower() & isupper()
islower() and isupper() methods checks if all characters of a given string are in lower case or uppercase respectively. It will return a boolean (True/False) data as a result of these methods.
str1 = "LEARN PYTHON PROGRAMMING FROM SAHIL FRUITWALA"
print(str1.islower()) #OUTPUT: False
print(str1.isupper()) #OUTPUT: True
str1 = "learn python programming from sahil fruitwala"
print(str1.islower()) #OUTPUT: True
print(str1.isupper()) #OUTPUT: False
str1 = "Learn Python Programming from @Sahil Fruitwala on 13-Oct 🙌"
print(str1.islower()) #OUTPUT: False
print(str1.isupper()) #OUTPUT: False
strip(), lstrip() & rstrip()
The standard definition of the strip() method is, that it returns a trimmed version of the string. But what does it mean? It means that the strip() method will return the string with extra white spaces removed from both ends of the string.
strip()
str1 = " LEARN PYTHON PROGRAMMING "
print(str1)
#OUTPU: LEARN PYTHON PROGRAMMING
print(str1.strip())
#OUTPU:LEARN PYTHON PROGRAMMING
lstrip() & rstrip()
lstrip() removes extra white spaces from the only left side of the string. Whereas, rstrip() removes extra white space from the right side of the string.
str1 = " LEARN PYTHON PROGRAMMING "
print(str1.lstrip())
#OUTPU:LEARN PYTHON PROGRAMMING
print(str1.rstrip())
#OUTPU: LEARN PYTHON PROGRAMMING
Note: You won't be able to see much difference but try it out on your system, and you will see the difference.
isalpha(), isnumeric() & isalnum()
How do you validate if the string contains only alphabets, or only numbers or alpha-numeric data?
Python provides you isalpha(), isnumeric() & isalnum() method to validate your strings.
str1 = "LearnPythonProgramming"
print(str1.isalpha()) # OUTPUT: True
print(str1.isnumeric()) # OUTPUT: False
print(str1.isalnum()) # OUTPUT: True
str1 = "Learn Python Programming"
print(str1.isalpha()) # OUTPUT: False
print(str1.isnumeric()) # OUTPUT: False
print(str1.isalnum()) # OUTPUT: False
str1 = "123LearnPythonProgramming123"
print(str1.isalpha()) # OUTPUT: False
print(str1.isnumeric()) # OUTPUT: False
print(str1.isalnum()) # OUTPUT: True
str1 = "123456"
print(str1.isalpha()) # OUTPUT: False
print(str1.isnumeric()) # OUTPUT: True
print(str1.isalnum()) # OUTPUT: True
str1 = "123LearnPythonProgramming123@"
print(str1.isalpha()) # OUTPUT: False
print(str1.isnumeric()) # OUTPUT: False
print(str1.isalnum()) # OUTPUT: False
split(), join() & replace()
The split() method splits a string into a list. This split is based on the character(s) passed as an argument in the split() method. If you don't pass any argument in the split method, it will take white space as the default value.
Definition of split() method: _string_.split(separator, maxsplit)
Here, maxsplit is optional argument. It specifies how many splits to do. Default value is -1, which is "all occurrences"
str1 = "Learn#Python#Programming"
print(str1.split("#"))
#OUTPUT: ['Learn', 'Python', 'Programming']
str1 = "Learn Python Programming"
print(str1.split()) #OUTPUT: ['Learn', 'Python', 'Programming']
print(str1.split(" ")) #OUTPUT: ['Learn', 'Python', 'Programming']
str1 = "Learn#Python#Programming#From#Sahil_Fruitwala"
print(str1.split("#", -1))
#OUTPUT: ['Learn', 'Python', 'Programming', 'From', 'Sahil_Fruitwala']
print(str1.split("#", 1))
#OUTPUT: ['Learn', 'Python#Programming#From#Sahil_Fruitwala']
print(str1.split("#", 3))
#OUTPUT: ['Learn', 'Python', 'Programming', 'From#Sahil_Fruitwala']
The join() method takes all items in an iterable and joins them into one string using a specified string or character(s). In the example below, we can see the iterable joined using white space.
myTuple = ("Learn", "Python", "Programming")
myList = ["Learn", "Python", "Programming"]
tuple1 = " ".join(myTuple)
list1 = " ".join(myList)
print(tuple1) # OUTPUT: Learn Python Programming
print(list1) # OUTPUT: Learn Python Programming
Using the join() method, we can join all keys of the dictionary as well.
myDict = {"Language":"Python", "Developer":"Sahil", "Year": 2021}
dict1 = " ".join(myDict)
# or
# dict1 = " ".join(myDict.keys())
print(dict1) # OUTPUT: Language Developer Year
We can join all the values of the dictionary using the following method:
myDict = {"Language":"Python", "Developer":"Sahil", "Year": "2021"}
dict1 = " ".join(myDict.values())
print(dict1) # OUTPUT: Language Developer Year
Note: You can join only string value using join() method.
The replace() method replaces a specified string or character(s) with another specified phrase.
myStr = "Python Programming"
newStr = myStr.replace("Python", "C++")
print(newStr)
We can pass the number of occurrences we want to replace. For example, if we want to replace only 2 occurrences of character 'a' the we can write the following code.
myStr = "Learn Python Programming from Sahil Fruitwala"
newStr = myStr.replace("a", "@", 2)
print(newStr)
Conclusion
10 minutes haaa! But finally, we are at the end of this section 😁.
I know, it is a lot to take in at a time. But, you don't need to remember everything that I have mentioned here. I just showed you so that you can recall what is possible and what is not. There are some other methods as well that I have not mentioned here.
If you want to find out more about string methods, check out W3School.
That was it. Thank you for reading. I know it is a lot but I hope you got some knowledge of string methods in Python.
Make sure to share any thoughts, questions, or concerns. Let me know if you need any help or want to discuss something. Reach out to me on Twitter or LinkedIn. Make sure to share any thoughts, questions, or concerns. I would love to see them.
Thank you for your precious time ❤
If you like this blog you can subscribe to my Newsletter for more amazing content.
Posted on August 16, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
June 19, 2024