Strings in Python
Now that you have learned how to work in interactive mode with Python is time to expand your knowledge and experiment with string objects.
Strings are sequences of characters and are coded with single quotes, double quotes or triple quotes. Why do we need strings? Strings are one of the most important data types in computer languages and they are used to keep track of textual information. Since strings are sequences of characters we can perform sequence operations on them, but we can not change the original strings because they are immutable like tuples, which means once defined they cannot be changed. Lists are not immutable, they can be changed. I will write about tuples and lists in the next articles.
Some of sequence operations we can perform on strings that will be explained in this article are: indexing, slicing, concatenation and repetition. These operations can be performed and will work on other sequences like lists and tuples. Strings have their own specific operations and these operations are called methods. For example a specific operation that can work only for strings can be uppercasing string’s letters. Logicaly we create an idea of methods and their purpose. Now, how about finding the length of a string? To find the length of a string we can use the builtin function len(), which is a function that will work on other sequences like list and tuples. Before we continue let me remind you that in order to save objects (strings in our case) and use them later we should assign them to variables; for example string = ‘unixmen’ like shown in Figure 1. We can use single or double quotes, it really doesn’t matter.
Figure 1- Assigning a string to our variable
Now we know from the previous article that when variables are used in expressions they return their actual values. So, if we put the variable string inside len() function like shown in Figure 2 we get the length of our string.
Figure 2 – Finding the length of our string with len() function
The plus sign means different operation on different object types, it means addition for numbers and concatenation for strings. Concatenation means joining two or more strings together, we do this with the plus sign like shown in Figure 3.
Figure 3 – Joining strings with the + sign
The same as with the + sign , the * means repetition for strings and multiplication for numbers. String repetition is shown in Figure 4.
Figure 4 – String Repetition
What is indexing? What about slicing? Indexing is used to fetch sequence components and slicing is a more advanced indexing; slicing is used to extract an entire section of the sequence. An index expression has this form : variable[index]. Index is a number greater or equal than 0 and less or equal than len(variable) -1. This is basic math and you understand that indexing in python starts from 0. The first item in the sequence has the index 0, the second item in the sequence has the index 1 and so on. If i want the first item in the ‘unixmen’ string i use ‘string‘ indexing expression where string is the variable assigned to ‘unixmen’ string value.
Figure 5 – Indexing
To perform slicing we use ‘variable[i:j]‘ expression. The slice starts from i position including it and stops at j, not including it. Figure 6 shows an example on how to use slicing.
Figure 6 – Slicing
Like us on Facebook
We need your assistance to stay live
This week Top Posts
- Top Things To Do After Installing Ubuntu 13.10 'Saucy Salamander' : Ubuntu 13.10 Saucy Salamander will be released on coming October 17th with many new salient featur...0 comments |
- News from Kaspersky Lab: Tor is protecting criminals : In a previous article I wrote a bit about Tor. Free browser, relays bouncing your IP all over the ...1 comment |
- Grive: An Unofficial, Open Source Linux Client For Google Drive : A couple of months before, we have featured a Linux client for Google Drive named Syncdrive, which w...2 comments |
- How To Access A LVM Drive / Partition in Ubuntu : Hey Guys, I installed Fedora on an external drive (LVM) recently and was trying to access it from U...0 comments |
- Adding and Deleting Cluster Resources (corosync pacemaker) : In our previous tutorial, we have shown you how to install corosync and pacemaker. We will show y...0 comments |
- Install Satis On Ubuntu Sever 13.10/13.04 : Satis is a simple static composer repository generator. It is used in any composer.json file as i...0 comments |
- Android Intercom: An Excellent Intercom App For Bikers Or Hiking Fans
- Install Google Talk Plugin In Fedora 20
- Adding and Deleting Cluster Resources (corosync pacemaker)
- News from Kaspersky Lab: Tor is protecting criminals
- Install Satis On Ubuntu Sever 13.10/13.04
- How To Access A LVM Drive / Partition in Ubuntu
- How To Disable Remember Open Files in Sublime Text 3
- Install Sublime Text 3 In Fedora Easily With Fedy
- Grive: An Unofficial, Open Source Linux Client For Google Drive
- Install nSnake Game In Terminal
This work by unixmen.com is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.
Copyright © 2008-2013 Unixmen.com .