Working with Strings in Python
What is the goal of this article?
We know that data is exploding and most of this data is in unstructured format. When I say unstructured format I refer to textual, audio, video and images. In this article we are going to look at the textual data only. It is crucial that you understand how to deal with strings to process your textual data better. You can practice simultaneously using the examples explained here in order to better understand the underlying concepts.
Which python version I am using for the following examples?
Python version 3.4.3 is used to explain the concepts covered in this article.
Why Work with Python?
There are pros and cons associated with every programming language and python is no exception. However, let us for the time being consider only the positives:
- Easy to understand and write
- Easy to follow syntax
- Free and full community support
First Program
Let's start with a simple example of printing 'Cheers Codementor Community' on the python console. In order to print ‘Cheers Codementor Community’ we just need to follow a simple piece of code:
message = "Cheers Codementor Community"
print (message)
This piece of code will print Cheers Codementor Community on your screen.
If you have programmed in other language say (C, C++ or VBA) earlier, you'll see that we didn’t have to declare the variable or variable type prior to using it. We simply defined the string by putting it within parenthesis and assigning that to a variable. You see how simple it is to program in python!!
String Operators: Adding and Multiplying
Let's start by exploring some common operators that are available in python to manipulate string variables. These predefined functions available in python deal with string variables and when used with strings they perform operations similar to their mathematical counterparts.
Adding the Strings: Concatenation
Concatenation means adding strings together and this is done using a ‘+’ sign. Let’s see a couple of example to get some idea:
Message 1 = ‘Cheers’ + ‘Codementor’+'Community'
print (Message1)
The output is printed as CheersCodementorCommunity. Now let's try and add some spaces in between differnt words:
Message2 = 'Cheers' + ' ' + 'Codementor' + ' ' + 'Community'
print (Message2)
The ouput is printed as Cheers Codementor Community
The blank space that we introduced in Message2 in between the two operators is also a string. It is important to highligh that the concatenation operator will work only when we are trying to add strings and it will fail if we concatenate with some other type of variables
Message3= 3 + 'Cheers' + ' ' + 'for' + ' ' + 'Codementor' + ' ' + 'Community'
This will results in error TypeError:Can't convert 'int' object to str implicitly
Multiply Strings: Creating Copies
Let's now see how we can create multiple copies of a string. This can be achieved using the multiply operator that is available in the python. Let us look at an example:
message1 = 'Three Cheers for Codementor Community\n'
message2 = ‘Hip ’ * 2
message3 = 'Hurray!!'
print(message1 + message2 + message3)
Note: the \n will introduce a line break
The above piece of code will results in the following output:
Three Cheers for Codementor Community
Hip Hip Hurray!!
Adding Text to Existing String: Appending
Sometimes you may want to append information to an already existing string and this is how we can do it.
message1 = 'Cheers'
message1 += ' '
message1 += 'Codementor'
message1 += ' '
message1 += 'Community'
print (message1)
This piece of code will result in the following output printed on your screeen:
Cheers Codementor Community
Other Methods to Manipulate Strings
There are many other methods that are available in python to work with strings. All these prebuilt functionalities can be combined in python to achieve your objectives while working with strings.
Length of the String
Sometimes you may want to determine the number of characters that are there in a string. Here we can use the length method to calculate the length of the string.
message1 = 'Cheers Codementor Community'
print(len(message1))
This piece of code will result in the following output printed on your screeen:
27
Search a Substring in a text
Sometimes you may want to search a substring in a text message. You can use FIND method to search the starting index position of that substring. This index can then be used for further task or processing. It is important to note that the indexing start from 0 and the matching is from left to right.
message1 = 'Cheers Codementor Community'
subtext1 = 'Code'
subtext2 = 'Word'
print(message1.find(subtext1))
print(message1.find(subtext2))
This piece of code will result in the following output printed on your screeen:
7
-1 (If the search term doesn’t appear in the string you’ll get a -1.)
String Case Conversion
Sometimes it is required that we convert the string to lower case or upper case based on the task at hand. This can be achieved using a very simple command as follows:
message1 = ‘Cheers Codementor Community’
message2 = message1.lower()
print(message2)
message3 = message1.upper()
print(message3)
This piece of code will result in the following output printed on your screeen:
cheers codementor community
CHEERS CODEMENTOR COMMUNITY
Sub-string Replacement
Many times you may want to replace a part of the string with some other sub-string. This task can be achieved using the replace method that is available in python.
message1 = ‘Kheers Kodementor Kommunity’
message2 = message1.replace('K','C')
print(mesage2)
This piece of code will result in the following output printed on your screeen:
Cheers Codementor Community
Slicing a String
Sometimes you may want to extract a part of the string and this is called as slicing the string. This will let to extract or remove a particular portion of the string. Let us look at an example
message1 = 'Cheers Codementor Community'
message2 = message1[0:6]
print(message2)
This piece of code will result in the following output printed on your screeen:
Cheers
Escape Sequence
Sometimes you may want to include some special characters in your string such as quotation mark (“). You can achieve this using the following method that is available in python. Let's say that you want to print 'Cheers "Codementor" Community'
message = 'Cheers \"Codementor\" Community'
print(message)
This piece of code will result in the following output printed on your screeen:
Cheers "Codementor" Community
There very bad idea use + when you need concatenate strings, its create many temporary strings in memory. The best way is to use format or f strings.
Last example with escaping wrong because in python you can use this without escaping:
message = ‘Cheers “Codementor” Community’
works perfectly.
but if you use double quotes than you need escaping
message = “Cheers \“Codementor\” Community”
Thanks for pointing out the errors!
Have made correction to the escaping part.
Regarding the use of + operator for concatenation, I appreciate your feedback, but this is a very basic tutorial for working with strings in python. I will prepare a more advanced post on the same topic and will include your suggestion for concatenation.
Thanks again,
Do point out if you find any other mistakes…
Simple, concise and we’ll explained
Thank you!!