Introducing various Python Strings

If you are interested to learn about the Python Numbers

Strings

Strings in python are surrounded by either single quotation marks, or double quotation marks. Like many other popular programming languages, strings in Python are arrays of bytes representing unicode characters. However, Python does not have a character data type, a single character is simply a string with a length of 1. Square brackets can be used to access elements of the string.

‘hello’ is the same as “hello”.

You can display a string literal with the print() function:

Example

print("Hello")<br>print('Hello')

Assign String to a Variable

Assigning a string to a variable is done with the variable name followed by an equal sign and the string:

Example

a = "Hello"<br>print(a)

Multiline Strings

You can assign a multiline string to a variable by using three quotes:

Example

You can use three double quotes:a = """Lorem ipsum dolor sit amet,
<br>consectetur adipiscing elit,
<br>sed do eiusmod tempor incididunt<br>ut labore et dolore magna aliqua."""<br>print(a)

Or three single quotes:

Example

a = '''Lorem ipsum dolor sit amet,
<br>consectetur adipiscing elit,
<br>sed do eiusmod tempor incididunt<br>ut labore et dolore magna aliqua.'''<br>print(a)

Note: in the result, the line breaks are inserted at the same position as in the code.

Strings are Arrays

Like many other popular programming languages, strings in Python are arrays of bytes representing unicode characters. However, Python does not have a character data type, a single character is simply a string with a length of 1. Square brackets can be used to access elements of the string.

Example

Get the character at position 1 (remember that the first character has the position 0):a = "Hello, World!"<br>print(a[1].

Looping Through a String

Since strings are arrays, we can loop through the characters in a string, with a for loop.

Example

Loop through the letters in the word "banana":for x in "banana":<br>  print(x)

String Length

To get the length of a string, use the len() function.

Example

The <code>len()</code> function returns the length of a string:a = "Hello, World!"<br>print(len(a))

Check String

To check if a certain phrase or character is present in a string, we can use the keyword in.

Example

Check if "free" is present in the following text:txt = "The best things in life are free!"<br>print("free" in txt)

Use it in an if statement:

Example

Print only if "free" is present:txt = "The best things in life are free!"<br>if "free" in txt:<br>  print("Yes, 'free' is present.")

Check if NOT

To check if a certain phrase or character is NOT present in a string, we can use the keyword not in.

Example

Check if "expensive" is NOT present in the following text:txt = "The best things in life are free!"<br>print("expensive" not in txt)

Use it in an if statement:

Example

print only if "expensive" is NOT present:txt =&nbsp;
"The best things in life are free!"<br>if&nbsp;
"expensive"&nbsp;not&nbsp;in&nbsp;
txt:<br>&nbsp;&nbsp;print("No, 'expensive' is NOT present.")

Python – Slicing Strings

Slicing

You can return a range of characters by using the slice syntax. Specify the start index and the end index, separated by a colon, to return a part of the string.

Example

b = "Hello, World!"
print(b[2:5])

Python – String Concatenation

String Concatenation

To concatenate, or combine, two strings you can use the + operator.

Example

Merge variable a with variable b into variable c:

a = "Hello"
b = "World"
c = a + b
print(c)

Python – Format – Strings

String Format

As we learned in the Python Variables chapter, we cannot combine strings and numbers like this:

Example

age = 36
txt = "My name is John, I am " + age
print(txt)

How to Access Characters in a String?

When you are manipulating sequences, it’s very common to have to access individual characters of string at one precise position. We call this indexing and you can access one character at a time by specifying its offset (position) inside square brackets after the string’s name. So, you get back a one-character string at the specified position, i.e offset.

<em> Accessing String Characters</em>
string6 = "PYTHON"
<strong>print</strong>(
&nbsp;

The expression inside the bracket is referred to as index and it is usually coded as an offset from the beginning of the string. The first offset (leftmost) is 0, the next is 1, 2, 3, and so on. Python also allows you to fetch items of a string using negative offsets. You can think of that as counting the string characters from the end. Basically, positive indexes count from the left, and the negative ones from the right. Let’s try to understand this with the help of a few examples.

Note: Remember that string indices must be integers, not float type

Access Characters in a STring
<em>#Example 6: String Indexing</em>
string1 = "I Love Python"
&nbsp;
<strong>print</strong>(string1[0])
<strong>print</strong>(string1[4], string1[5])
<strong>print</strong>(string1[-1], string1[-3])
<em>#print(string1[20])</em>
&nbsp;

The points that we can infer from the above example are:

  • The first offset of a string is at 0
  • string1[0] fetches the first item of the string, in our example ‘I’
  • Negative indexing can be considered as counting backward from the end or right
  • string1[-3] fetches the third item from the end (like string1[len(string1)−2]), in our example ‘h’
  • If you specify an offset that is the length of the string or longer you will get an exception, in our example the commented line string1[20] raises exception

In the examples that we have worked on till now, we did encounter a few unknowns like + operator, or len() function, etc. Therefore, let us take a look at a few common string operations that you will frequently come accross.

Python String Operations

How to Find the String Length?

Let’s begin by calculating the length of the string. Strings like any other Python sequence have a length. You can easily get the length of the string using the built-in function len as shown below. len is a built-in function in Python that returns the number of characters in a string.

<em>#Example : String length using len function</em>
string1 = "I Love Python"
<strong>print</strong>(len(string1))
<em>#print(string1[len(string1)])</em>

Like I mentioned earlier, while indexing if we use the offset that is the length of the string, we get IndexError. That is because since we start indexing from 0, the last letter of the string will be at position one less than string (length-1). If you ask me, you can always use negative indices. Alternatively, you can also find the length of the string by looping through a string. Let’s take a look

Looping Through a String

Looping means being able to execute a block of code more than once. Python provides different looping constructs such as for and while, which serve different purposes. Often, a lot of string operations involve processing a string one character at a time and we call this traversal. Here;s an example demonstrating who to traverse through a string:

<em>#Example : Looping through a string</em>
"""Finding the length of a string using for loop"""
string1 = "I Love Python"
<strong>def</strong> calculatelen(string):
  count = 0
  <strong>for</strong> i <strong>in</strong> string:
    count = count + 1
  <strong>return</strong> count
&nbsp;
<strong>print</strong>(calculatelen(string1))

In the above example, we are calculating the length of the string using for loop and in operator. Each time the loop traverses the string, the variable count is increased by 1. The loop continues until we reach the end of the string. Let’s now look at a more interesting example.

Python Program to Check if the Given String is an Abecedarian Series

Write a Python program to check if the given string is an abecedarian series. Abecedarian series refers to a sequence or list in which the elements appear in alphabetical order. Example: “ABCDEFGH” is an abecedarian series, while “BOB” is not.

<em>#Example : Abecedarian Series</em>
<strong>def</strong> is_abecedarian(string):
    previous = string[0]
    <strong>for</strong> i <strong>in</strong> string:
        <strong>if</strong> i &amp;lt; previous:
            <strong>return</strong> False
        previous = i
    <strong>return</strong> True
&nbsp;
<strong>print</strong>(is_abecedarian('BCDEFGH'))
<strong>print</strong>(is_abecedarian('BOB'))

In the above example, you should notice that the relational operators work on strings. In the fourth line of the code, if i < previous, we are comparing two string characters using < operator. Now let us consider an example using a while loop construct. Try writing a Python program that prints each character of each item of string in a new line using a while loop.

<em>#Example : Print each character of a string in new line using while loop</em>
"""Finding the length of a string using for loop"""
string1 = "I Love Python"
<strong>def</strong> displaystring(string):
  count = 0
  <strong>while</strong> count &amp;lt; len(string):
    <strong>print</strong>(string[count])
    count = count + 1
&nbsp;
displaystring(string1)

This is a pretty simple example. The loop iterates until the variable count is less than the length of the string. When the count equals the length of the string, the condition evaluates to False, and the body of the loop is not executed anymore.

Concatenation of Two or More Strings

You can concatenate two or more strings using + operator and repeat a string using * operator like shown below.

#Example : Concatenation and repetition of strings
string1 = 'abc' + 'def' 
string2 = 'xyz'
string3 = string1 + string2  #concatenation
string4 = string2 * 4  #repetition
print(string3)
print(string4)
 

Python Program Demonstrating Concatenation of Strings

Here’s a simple exercise. In Robert McCloskey’s book Make Way for Ducklings, the names of various ducklings goes like this: Jack, Kack, Lack, Mack, Nack, Ouack, Pack, and Quack. Given prefixes “JKLMNOPQ” and suffix “ack”, write a python program that generates these names using loop constructs and concatenation operators

#Example: For loop and + operator
prefix = "JKLMNOPQ"
suffix = 'ack'
for i in prefix:
  if (i == 'Q' or i == 'O'):
     print(i + "u" + suffix)
  else:
     print(i + suffix)
 

Let us now look at how to change or delete a string in Python. Is that even possible? What’s your answer: Yes or No?

How to Update or Delete a String in Python?

Updating a String

If you have guessed it ‘Yes’, then you are wrong. Python does not allow you to update or delete a character of a string. That’s because strings are immutable sequence types of Python, which means you can’t change an existing string in-place. The best you can do is to create a new string to achieve the variation.

#Example : Updating or Deleting a String
"""Strings are immutable data types"""
string1 = "I love python"
string1[0] = "Y"
print(string1)
 
Update or Delete String in Python

Trying to update a string will give you the TypeError as shown above. So how can you modify a string in Python? You can do so using string tools like concatenation or slicing and then assigning all this to a new string variable with the same name as that of the original string. We will look at slicing operations in the next part of the article.

#Example : Updating entire string
string2 = "I Love Python"
print("Original String:", string2)
string2 = "I Love Java"
print("Updated String:", string2)
 

Deleting a String

You can delete an entire string using the del keyword in Python. However, you can delete a character of a string as shown below.

#Example : Deleting a character of string
string1 = "I love python"
print("Original string:", string1)
del(string1[3])
print("Deleting the string item at 3rd offset:", string1)
 
 
#Example 16: Deleting entire string
string2 = "I Love Python"
print("Original String:", string2)
del(string2)
print("Deleted String:", string2)
 

Example will give you a TypeError and Example 16 will give you NameError. Let us now move on to the next query.

How to Check if the Text is in the String?

There are multiple ways to search a string for a particular substring or a character. The simplest way to check if a string contains a substring in Python is using in operator.

#Example : Method 1, Searching using in operator
string1 = "I Love Python"
word = "Love"
if word in string1:
  print("Word Found!")
else:
  print("Not found what you are looking for in the string")
 
word1 = "love"
#For case-insensitivity you can use 'if word1 in string1.lower():'
if word1 in string1:      
  print("Word Found!")
else:
  print("Not found what you are looking for in the string")

Beware of case-sensitivity. If you want to do a case-insensitive search you can normalize the strings using lower() or upper() built-in functions of Python as shown above. You should keep in mind that the above method works fine for sequences of characters, but not necessarily a whole word. For example, “word” in “sword” is True. If you want to match the entire word then you should use regular expressions.

Now let see how to search if a particular character is present in a string

#Example : Using find() method
string1 = "I Love Python"
word = "kove"
if string1.find(word) != -1:
  print("Word Found!")
else:
  print("Not found!")

The find() method searches for the word or a character and returns the position of the substring in the original string if the substring is present. Else, returns -1 if the substring is not found.

Moving on, let’s explore how to format Python strings.

Formatting Strings in Python

Proper text formatting makes code and data much easier to read and understand. Python offers a rich variety of string formatting methods. String formatting allows you to perform multiple type-specific substitutions on a given string in a single step. In Python, string formatting is available in two ways:

  1. String formatting expressions:  ‘…%s…’ % (values)
  2. String formatting method calls: …{}…’.format(values)

Let us explore each of these in detail.

String Formatting Expressions

This formatting technique has been there since Python’s inception and loosely based on printf statement of C language. Python provides the binary operator %, which can be applied on strings to format them in a very simple manner. Using %, you can write code to perform multiple string substitutions all at once rather than doing it in parts. Let’s look at that with the help of an example:

#Example : string formatting expressions: ‘…%s…’ % (values)
print("%d %s %s %0.1f" % (1, ‘use’, ‘Python’, 3.0 ))

Doesn’t that make sense? No problem! String formatting examples might look a little strange until you understand the syntax or what is actually being done there. So, let’s dive in. To format strings:

  1. On the left side of the % operator, you need to provide a format string containing one or more conversion targets, each of which starts with %
  2. On the right, you need to provide a Python object that you want to be inserted in the format string

For instance, in the above example we have %d, %s, %f as our conversion targets and to the right of the % operator we have the integer 1 that replaces %d, the strings ‘use’ and ‘Python’ that replace the two %s, and finally 3.0 that replaces %0.1f. The general format of the conversion targets looks something like this:

%[(keyname)][(flags)][(.precision)]typecode
String Formatting

Output:

String Formatting Output

You can learn more about this printf style string formatting in the official Python Documentation.

String Formatting Method Calls

Python 2.0 7 Python 3.0 introduced a new way of string formatting that provided a large degree of flexibility and customization compared to C printf style formatting.

Python String format() Method

Python’s str.format() can be used to perform string formatting operations. It’s more like a normal function call rather than an expression. The format string contains substitution targets (replacement fields) and arguments to be inserted either by position (e.g., {2}) or keyword (e.g., {sam}). Just like you would pass arguments to functions and methods by position or keyword. Here’s an example to clear the picture.

#Example : str.format method
str1 = "I"
str2 = "Love"
str3 = "Python"
print(‘{} {} and {}’.format(str1, str2, str3))
print(‘{0} {1}, {0} {2}’.format(str3, 2.0, 3.0))
print("{0} {ver2}. {0} {ver1}".format(str3, ver1=2.0, ver2=3.0))
  • In the line (‘{} {} and {}’.format(str1, str2, str3), you can see that bracket pairs are replaced with the arguments in the order in which you pass arguments.
  • Like shown in the next line of the code, (‘{0} {1}, {0} {2}’.format(str3, 2.0, 3.0, indexes can be specified inside the brackets.
  • You can also use named arguments like in the last line of the code, (“{0} {ver2}. {0} {ver1}”.format(str3, ver1=2.0, ver2=3.0)
#Example : String Formatting Examples
str3 = "Python"
template = ‘{0} {1} {2}’ # By position
print(template.format(‘I’, ‘Love’, str3))
print(‘{0:f}, {1:.3e}, {2:g}’.format(3.14159, 3.14159, 3.14159))
print(‘{0}:{ver}’.format(str3, ver=[2.0, 3.0]))
 
tuple1 = (334, 45, 23456, 103, 66)
print(‘{0} {2} {1} {2} {3} {2} {1} {2}’.format(*tuple1))
 
#Alignment &amp; padding
print(‘{:~^30s}’.format(‘Python’))
print(‘{:~&gt;30}’.format(‘Python’))
print(‘{:0=8d}’.format(–1234))

The points that can be inferred from the above example are as follows:

  • You can pass arguments by position or keyword name
  • format() methods creates and returns a new string object
  • You can use the format method just % operator by adding extra syntax in the format string. Ex: ‘{0:f}, {1:.3e}, {2:g}’.format(3.14159, 3.14159, 3.14159)
  • Unlike %, format allows repetition of arguments as shown in the above example, ‘{0} {2} {1} {2} {3} {2} {1} {2}’.format(*tuple1)
  • Note that, you can use pass dictionary keys, tuples, lists, and other objects as arguments to format method
  • The format method also allows you to change the alignment of the string using the syntax :[fill_char][align_operator][width]. The align_operator can be:
    • <, for left-alignment within the given width
    • >, for right-alignment within the given width
    • ^, for center alignment
    • =, to place the padding after the sign

(Refer to the last three line of the code)

Escape Character

In Python, you can escape the meaning of some characters within the strings to achieve your requirement. Python allows you to do that by using a backslash (\). Backslash and the character immediately following it together from an escape sequence. The most common escape sequence that you might have come across would be \n, which indicates to begin a new line.

#Example: Escape Characters
print(‘Welcome\nto\n\nPython Tuorial!\n‘)
print(‘a\tbc’)
print(‘\"I love Python!\"‘)
print(‘Today we are learning: \\Python\\‘)
Escape SequenceDescription
\nInserts a new line
\tInserts a horizontal tab
\\Inserts a literal backslash in a string
\’ & \”Insert a single quote or double quote in a string

That’s great! But what if you want to ignore escape characters? How to do that?

How to Ignore Escape Characters?

You can ignore escape sequences in Python, by passing the string as raw string. This can be done by placing ‘r’ or ‘R’ before the string that you are working on. ‘r’ stands for raw and it interprets the string as and preserves the escape sequences as literals.

#Example : Ignoring Escape Sequence
print(r‘Welcome\nto\n\nPython Tuorial!\n‘)
str2 = r"\"Hello world\""
print(str2)

Note: Know that unescaped backslash characters at the end of the raw string will cause error. Example: r”Hello world\”

Built-in String Methods

Python provides a large set of string methods. I have tried to cover the most common string methods in the example below.

#Example : String Methods
str1 = ‘\t \n This is a Python Tutorial. \t\t \n‘
print(str1.strip())   #Striiping Whitespace
str2 = "XßΣ"
str3 = ‘python’
str4 = ‘THIS is python’
print(str2.lower())    #converts to lowercase
print(str3.upper())    #converts to uppercase
print(str2.casefold())   #creates lowercase string(might cause strings to grow in length)
print(str3.capitalize()) #returns capitalized version of string
print(str4.swapcase())   #swaps the case
 
str6 = ‘1s’
str7 = " "
str8 = ‘this is python’
print(str6.isdigit())
print(”.join(reversed(str3))) #takes a string &amp; returns an iterator in reverse order;
print(str4.replace(‘python’, ‘java’)) #replace one sub-string with another sub-string
 
# Methods to evaluate the contents of a string
print(str4.isalpha()) 
print(str3.islower())
print(str3.isupper())
print(str6.startswith("1"))
 
print(str7.join(["python","and","java"])) #join a list of strings using another string as separator
print(str8.count("th"))  #count number of occurrences of a sub-string
MethodDescription
str.strip([chars]Removes (strips) any leading or trailing characters contained in the argument chars. By default removes all white spaces.
str.rstrip & str.lstripStrip characters from the end of the string and the from the start of the string respectively
str.lower()Converts every character to its lowercase equivalent
str.upper()Converts every character to its uppercase equivalent
str.casefold()Creates a lowercase string but is more aggressive than str.lower and cause strings to grow in length
str.capitalize()Capitalize the string, i.e makes first character capital and rest small
str.swapcase()Swaps lowercase character with uppercase and vice versa
str.title()Returns title case version of the string
str.split(sep=None, maxsplit=-1)Returns list of substrings based on the separator argument sep
str.replace(old, new[ , count])Replaces the old string with the new string. Optional argument specifies number of occurrences to be replaced
str.isupper(), str.islower(), str.istitle(), str.isalpha(), str.isalnum(), str.isspace()These methods can be used to evaluate the contents of the string
str.join(‘ ‘,  ‘ ‘,  ‘ ‘ …)A string separator can used to join a list of strings together into a single string
str.count(sub[ , start[ , end]])Counts the number of occurrences of a substring. You can also specify the starting & ending positions
str.startswith(prefix[ , start[ , end]]) & str.endswith(prefix[ , start[ , end]])These methods can be used to test the beginning and the ending of the string respectively

So, folks, these are just a few string methods. I suggest you go through official Python documentation to learn about these string methods and others in detail.

Introducing various Python Strings
Show Buttons
Hide Buttons