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
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 |
- OpenLDAP Installation and Configuration in Ubuntu 12.10/13.04/13.10 And Debian 6/7 : OpenLDAP is a free open source Light Weight Directory Access protocol developed by the OpenLDAP proj...0 comments |
- Configure Your Browser To Use Tor On Ubuntu/Debian/Linux Mint : Tor, The Onion Router, is a network of Virtual Tunnels that allows users to communicate securely and...2 comments |
- How To Upgrade From Ubuntu 13.04 Raring To Ubuntu 13.10 Saucy Salamander : Ubuntu 13.10 Saucy will be released on October 17th. Hope it will come with lot of improvements and ...0 comments |
- Install lamp with 1 command in Ubuntu 12.10, 13.04 Raring Ringtail & LinuxMint13 : Updated: 10/09/2012 :LAMP (Linux, Apache, MySQL and PHP) is an open source Web development platform ...0 comments |
- Setup A Full Featured ITIL Management System Using Integria IMS On CentOS 6 : Integria IMS is a fully featured ITIL management system, featuring a ticketing system, inventory/C...2 comments |
- OpenLDAP Installation and Configuration in Ubuntu 12.10/13.04/13.10 And Debian 6/7
- Configure Your Browser To Use Tor On Ubuntu/Debian/Linux Mint
- Setup A Full Featured ITIL Management System Using Integria IMS On CentOS 6
- Install LibreOffice 4.1.3 in Elementary OS ‘Luna’
- How To Install Simple Scan in Crunchbang ‘Waldorf’
- Selene Media Encoder: Convert Audio, Video Files To Most Popular Formats
- Linux Kernel 3.12.2 Is Available For Download! Install / Upgrade Instructions
- CentOS 6.5 Has Been Officially Released!
- Format Junkie: Convert Media Files To All Popular Formats
- Setup Squid Proxy Server On openSUSE 13.1
This work by unixmen.com is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.
Copyright © 2008-2013 Unixmen.com .