Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
October 11, 2021 05:24 pm GMT

Fun with Fantastic String Data Type in Python

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 kind of data.

It's Show Time
By GIFs Ninja

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 from where to start the operation, stop suggests where to stop the operations and step suggests what step (1, 2, -3, -1, etc.) to take. Slice operator can be used with string, list and tuple. 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.

Frame 1

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

Frame 2

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 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

Alt Text

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

Alt Text

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

Alt Text

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 of using just str1[:] or str1[::]. It will return the same string.

str1 = "Python Programming"print(str1[::3])    #OUTPUT : Ph oaiprint(str1[::])     #OUTPUT : Python Programmingprint(str1[:])      #OUTPUT : Python Programming

Alt Text

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, 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

Alt Text

str2 = "Python Programming"print(str2[-5:-1])  #OUTPUT : nmagr

Alt Text

str2 = "Python Programming"print(str2[-1:-5:-1])   #OUTPUT : gnim

Alt Text

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 which you might use often.

  1. capitalize(), title() & count()
  2. find() & index()
  3. lower() & upper()
  4. islower() & isupper()
  5. strip(), lstrip() & rstrip()
  6. isalpha(), isnumeric() & isalnum()
  7. split(), join() & replace()

capitalize(), title() & count()

capitalize() method converts first character of string to Capital Letter. Whereas, title() method capitalize first character of every word in string.

str1 = "learn python programming"print(str1.capitalize())    #OUTPUT: Learn python programmingprint(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: 2print(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: 0print(str1.find(' '))          #OUTPUT: 5print(str1.find('z'))         #OUTPUT: -1

index()

str1 = "learn python programming learn"print(str1.index('learn'))  #OUTPUT: 0print(str1.index(' '))      #OUTPUT: 5print(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 for assignment of multi-line string data.

My personal 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: Falseprint(str1.isupper()) #OUTPUT: True
str1 = "learn python programming from sahil fruitwala"print(str1.islower()) #OUTPUT: Trueprint(str1.isupper()) #OUTPUT: False
str1 = "Learn Python Programming from @Sahil Fruitwala on 13-Oct "print(str1.islower()) #OUTPUT: Falseprint(str1.isupper()) #OUTPUT: False

strip(), lstrip() & rstrip()

The standard definition of the strip() method is, it returns a trimmed version of the string. But what does it mean? It means, 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, 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: Trueprint(str1.isnumeric()) # OUTPUT: Falseprint(str1.isalnum())   # OUTPUT: True
str1 = "Learn Python Programming"print(str1.isalpha())   # OUTPUT: Falseprint(str1.isnumeric()) # OUTPUT: Falseprint(str1.isalnum())   # OUTPUT: False
str1 = "123LearnPythonProgramming123"print(str1.isalpha())   # OUTPUT: Falseprint(str1.isnumeric()) # OUTPUT: Falseprint(str1.isalnum())   # OUTPUT: True
str1 = "123456"print(str1.isalpha())   # OUTPUT: Falseprint(str1.isnumeric()) # OUTPUT: Trueprint(str1.isalnum())   # OUTPUT: True
str1 = "123LearnPythonProgramming123@"print(str1.isalpha())   # OUTPUT: Falseprint(str1.isnumeric()) # OUTPUT: Falseprint(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 Programmingprint(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 on 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. If you have any questions or doubt mention them in the comment section or reach out to me on Twitter, LinkedIn or Instagram.

If you want me to explain any specific topic, mention that in the comment section below .


Original Link: https://dev.to/sahilfruitwala/fun-with-fantastic-string-data-type-in-python-2h5j

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To