Strings are central and important components in any programming language and they are needed in almost any type of application (commercial, scientific, medical, etc..). Think of all things you can store in a String such as names, addresses, communication data and much more.
Therefore, it is important to get to know how to use and manipulate strings in the Python programming language.
This article has been inspired by the book Python Crash Course: A Hands-On, Project-Based Introduction to Programming (click to check to Amazon). I have studied Python from this book and I find it a great source both for experience programmers and for beginners who are interested in learning Python. So please check it out to help the book’s author and me to write new and great content for you.
Initializing a String
Strings in Python can be created either by assignment or by initializing the string inside our program’s code. Python provides us with flexibility to create Strings in three different ways.
First, we can initialize a string by surrounding text with double quotes. This is similar to what you would do in Java or C++.
doubleQuote = "String with double quotes"
You can view the contents of a string by using the print function.
print(doubleQuote)
Running the two statements above together would result in the following output.
String with double quotes
We can also initialize a string by using single quotes.
SingleQuote = 'String with single quote'
This is similar to what you would do in Javascript and would yield the same result as using double quotes. However, the disadvantage of this is that Python would not be able to differentiate between a single quote which marks the end of the string, and an apostrophe which is used in the middle of the sentence. Consider something like this:
myString = 'Charlie's sandwich' # We get a error here
You can solve such issues by using escape characters. More on that later. Now, let us check out how to create a string using three double quotes.
tripleQuote = """String with triple quotes"""
In Python, you can create strings by starting and ending the text with “””
This allows you to create multiline strings in your code without having to use the newline escape character \n. For example:
multilineString1 = "This is a line.\nAnd this is another line."
multilineString2 = """This is a line.
And this is also another line :)"""
print(multilineString1)
print(multilineString2)
Running the program above will yield the following output.
This is a line.
And this is another line.
This is a line.
And this is also another line :)
String concatenation
In Python, strings can be concatenated (appended together) by using the + sign.
string1 = "A string"
spaceString = " "
string2 = "and another string"
concatenatedString = string1 + spaceString + string2
print(concatenatedString)
Running the program above will yield the following output.
A string and another string
Escape characters
Escape characters are sequences of characters that are used to represent special characters which are difficult to represent inside a code file or would otherwise cause an ambiguous interpretation inside our code. Such special characters include line breaks, tabs, quotation marks among many others.
From our previous example, you can see why it would be a problem if we decide to add a single or a double quote character in the middle of the string. This is because the Python interpreter would not be able to tell the difference between a quote that ends a string and one which is meant to be in the middle of the string.
Let us see a few examples of some escape characters in action.
stringWithQuote = "Hello there. This \" is a double quote"
StringWithSingleQuote= 'And this \' is called a single quote'
StringWithTabs="""Line 1
\tLine 2
Line 3
\tLine4"""
print(stringWithQuote)
print(StringWithSingleQuote)
print(StringWithTabs)
- \” : Represents a ” in the middle of a string
- \’ : Represents a ‘ in the middle of a string
- \t : Represents a tab
If we run the program above, we would get the following result:
Hello there. This ” is a double quote
And this ‘ is called a single quote
Line 1
Line 2
Line 3
Line4
You can find a complete reference of all possible escape characters by checking the official Python documentation here.
Commonly used string manipulation functions
Python also provides us with a wide range of built-in functions that we can apply to strings in order to get a desired effect. Let us go through some of the most common ones.
upper
upper is a string function which can be used to provide a version of the string with all characters are in upper-case.
randmonCase = "THis senTenCe is WritTen randomly IN uPPer aND loWER CAsE!"
print(randmonCase)
print(randmonCase.upper())
This program will result in the following output.
THis senTenCe is WritTen randomly IN uPPer aND loWER CAsE!
THIS SENTENCE IS WRITTEN RANDOMLY IN UPPER AND LOWER CASE!
Please keep in mind that the call randomCase.upper() does not change the contents of the origianl string randomCase. If you would like to store the new value, then you have to save the output into a new variable.
upperCase = randmonCase.upper()
lower
lower is a string manipulation function which is used to provide a version of the string where all characters are in lower case.
randmonCase = "THis senTenCe is WritTen randomly IN uPPer aND loWER CAsE!"
lower = randmonCase.lower()
print(randmonCase)
print(lower)
And the output of the above program will be.
THis senTenCe is WritTen randomly IN uPPer aND loWER CAsE!
this sentence is written randomly in upper and lower case!
title
title is a String function which can be used to transform each word in a string into a word which begins with an upper case character, followed by lower case for the rest of the characters in the word.
For example, the string “hello” would be converted to “Hello” and the string “WHAT” will be converted to “What”. This transformation would then be applied to every word in your string. For example:
randmonCase = "THis senTenCe is WritTen randomly IN uPPer aND loWER CAsE!"
titleCase = randmonCase.title()
print(randmonCase)
print(titleCase)
And the output will be.
THis senTenCe is WritTen randomly IN uPPer aND loWER CAsE!
This Sentence Is Written Randomly In Upper And Lower Case!
strip, lstrip & rstrip
The strip function is a string function in Python that is used to “trim” a string from whitespaces at the start and the end of the string. Whitespaces are non-printing characters, such as spaces, new lines and tabs.
While the strip function will remove such spaces from the beginning and the end of the string, the lstrip function will remove whitespaces from the left side (beginning) of the string and rstrip will remove whitespaces from the right side (end) of the string.
Let us test these functions by example.
stringWithSpaces = "\t Hi there \t"
stringStrip = stringWithSpaces.strip()
stringLStrip = stringWithSpaces.lstrip()
stringRStrip = stringWithSpaces.rstrip()
print(stringWithSpaces)
print(stringStrip)
print(stringLStrip)
print(stringRStrip)
Running the program above will yield the following results.
Hi there
Hi there
Hi there
Hi there
split & rsplit
The functions split and rsplit are used to split a string into an array of substrings based on a selected separator. By default, the split function will split a string by spaces. For example:
someString = "abcde fghijk lmnopq"
print(someString.split())
This program will print the following result:
['abcde', 'fghijk', 'lmnopq']
If your string should be split by something other than a space, then you can add this as an argument to the split function. For example, let us split a string made up of comma separated words.
someString = "abcde,fghijk,lmnopq"
print(someString.rsplit(","))
The result would be:
['abcde', 'fghijk', 'lmnopq']
So you might ask, what is the difference between split and rsplit. split and rsplit both split a string, however, split will start the “splitting” process from left to right, while rsplit will start splitting the string from right to left.
This difference is noticeable when you define the number of splits that you want the function to execute. Both the split and rsplit function support a number as an argument which defines the number of splits that it will perform on a string. Let us revisit the previous example by limiting the number of splits to 1.
someString = "abcde,fghijk,lmnopq"
print(someString.split(",",1))
print(someString.rsplit(",",1))
This will cause the function to stop once the first separator is encountered. The result of the program above will look as follows.
['abcde', 'fghijk,lmnopq']
['abcde,fghijk', 'lmnopq']
Notice the first result stopped splitting from the left side while the second result split the string from the right side.
Finding the length of a string
In Python, we can find the length of a string by using the len function. The string is passed as an argument to the len function in order to get the number of characters in the string.
theString = "1234567890"
print(len(theString))
10
Process finished with exit code 0
Slicing
Slicing is a useful functionality in Python where subsequences and be derived from lists. Since a string can also be thought of as a list of single character strings, it can also be sliced into smaller subsequences.
For example, to get a substring of the first 3 characters in a string, you can indicate it by [0:3]. where 0 is the starting index (inclusive) and 3 is the end index (exclusive).
You can also fetch the character that resides in the index position 3 (since indices start from 0, this will be the 4th character) by indicating it as [3].
theString = "1234567890"
print(theString[0:3])
print(theString[2:3])
print(theString[3])
Let us check the output of the program above.
123
3
4
Summary
In this article, we discussed how to create strings in Python. We discussed concatenation, transforming strings into upper and lower case, getting the length of a string and slicing.
If you made it this far into the article, then I would like to say thank you 🙂 and I hope to see you again soon!