Strings in Python

In this tutorial, I am going to discuss about String datatype in python, different operations which can be performed on strings and built-in functions available in python for strings.

Strings in Python

A string is a sequence of characters in python which are enclosed in single quotes ( ' ), double quotes ( " ) or triple quotes ( ' ' ' ). The triple single/double quotes : ( ''' ) or ( """ ) are generally used to represent multi-line strings (spanning a string across multiple lines) in python but they can also be used to represent simple strings.

Look at the below examples which show string declaration :

str1 = 'Hello'
str2 = "Welcome"
str3 = '''To Learn'''
str4 = """Python     
programming""" # Multiline string

print(str1,str2,str3,str4)
Hello Welcome To Learn Python     
programming

However, make sure that you use same quotes at start and end of the string declaration.

Accessing a part of string

To access a single character or a part (sub-string) from the string, the indexing operator ( [ ] ) and slicing operator ( [ : ] ) is used.

In python, indexing of a sequence starts from 0 and increments by 1 after each element of the sequence.

The indexing [ ] operator takes positive integers as indices and provides the value present at that index, these indices start from 0. We can also provide negative integers which point to the values from end of a string.

The Slicing operator [ : ] take two numbers and gives the sub-string between these indices.
Let's see some examples.

Displaying a single character from the string :

str1 = "Welcome"
print(str1[3])
print(str1[-1])
c
e

Slicing through the string to get sub-strings :

str2 = "Python programming"
print(str2[4:])
print(str2[:7])
print(str2[-4:])
print(str2[:-8])
print(str2[3:9])
on programming
Python 
ming
Python pro
hon pr

If we use an index which is not in the range of the string or use numbers other than integers then we will get error as shown in the below examples :

a = "Hippopotamus"
print(a[15])
---------------------------------------------------------------------------

IndexError                                Traceback (most recent call last)

< ipython-input-36-2e21ab6ae394 > in < module >
      1 a = "Hippopotamus"
----> 2 print(a[15])

IndexError: string index out of range
print(a[4.7])
---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

< ipython-input-37-b104095df90b > in < module >
----> 1 print(a[4.7])

TypeError: string indices must be integers

Immutability of String in Python

In python, string objects are immutable which means that we cannot update any string object. Whenever we perform any operations like concatenation or repetition, python creates a new string object every time. Even if we try to update a string using indexing operator, TypeError will be shown.

var = "SunLight"
print(var)
var[1] = 'e'
SunLight

---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

< ipython-input-137-4aa43ff8ddd3 > in < module >
      1 var = "SunLight"
      2 print(var)
----> 3 var[1] = 'e'

TypeError: 'str' object does not support item assignment

In the above example, python throws TypeError as string object does not support item assignment. Even if we try to update the string using indexing operator.

Quotes inside a Python string

As you know, quotes are used to determine the start and end of strings in python which is also known as delimiting the string.

var = "He said, 'We will be coming home late in the evening'."
print(var)
var1 = 'I like to play "Guitar" '
print(var1)
var2 = ''' 'What' 'are' "you" 'doing' ? '''
print(var2)
He said, 'We will be coming home late in the evening'.
I like to play "Guitar" 
 'What' 'are' "you" 'doing' ? 

In the above example, we can see that when we want use quotes in a python string, we have to make sure that we delimit the string using another type of quotes than which we want to use inside the string.

Traversing through a string in python

We have learnt about for and while loop in python. These can be used to traverse/iterate through a string which means passing through each character present in the string. This is useful when we want to work with characters in the string.

Let's see how to do that.

str1 = "Hello, I am Ritu"
for i in str1:
    print("Character : ",i)
Character :  H
Character :  e
Character :  l
Character :  l
Character :  o
Character :  ,
Character :   
Character :  I
Character :   
Character :  a
Character :  m
Character :   
Character :  R
Character :  i
Character :  t
Character :  u

In the above example,python also counts blank space as a character of the string. This can also be used if we wish to calculate number of a specific character from the string.

Let's see how to do that :

count = 0 
for i in str1:
    if(i == 'l'):
        count = count + 1
print("There are",count,"'l' characters in the string.")
There are 2 'l' characters in the string.

Python String Operations

Python provides us with different operations that can be performed on strings, thereby making strings the most used and versatile datatypes in python.

Let's learn about these different operations possible on python strings.

Concatenation Operation

Concatenation of strings mean joining of two strings to form a new string. This operation is performed using the + operator in python. We can also concatenate two string literals in python by simply writing them together.

The + operator takes two strings as operands and joins them together as output.

Let's see some examples.

a = "Concat" 'enate'  # here "concat" and 'enate' are two string literals
b = "Coding"
c = " is Fun"
print(a)
print(b+c)
d = 3
print(b+d)
Concatenate
Coding is Fun

---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

< ipython-input-45-1b2974a8c3f7 > in < module >
      5 print(b+c)
      6 d = 3
----> 7 print(b+d)

TypeError: can only concatenate str (not "int") to str

In the above example,when we use an integer with a string for concatenation, python gives us TypeError which means that only string type can be used with + operator for concatenation.

Deleting a String

Python has a keyword del, which is used to delete a python object. As you know, in python everything is an object including strings. To delete a string we use this del keyword as shown in below example.

strin = "This is a string"
print(strin)
del strin
print(strin)  #Gives error as strin is deleted.
This is a string

---------------------------------------------------------------------------

NameError                                 Traceback (most recent call last)

< ipython-input-1-98d545357132 > in < module >
      2 print(strin)
      3 del strin
----> 4 print(strin)  #Gives error as strin is deleted.

NameError: name 'strin' is not defined
strin1 = "Second string"
del strin1[3]
---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

< ipython-input-16-14fdda9ea50d > in < module >
      1 strin1 = "Second string"
----> 2 del strin1[3]

TypeError: 'str' object doesn't support item deletion

In the above example,in the second example, we are not able to delete a particular item or character from the string strin1. This again proves that Strings are Immutable in python.

Repeating a string

The * operator in python can be used to repeat a string multiple number of times. It takes a string and a number as operands, the number is the number of times the string should be repeated.

Just like + operator, the * operator also gives TypeError if we do not use an integer and a string as operands.

Let's see some examples below :

str1 = "welcome "
print(str1*3)
print(str1*2.3)
welcome welcome welcome 

---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

 in 
      1 str1 = "welcome "
      2 print(str1*3)
----> 3 print(str1*2.3)

TypeError: can't multiply sequence by non-int of type 'float'

Membership Operation

We have discussed about membership operators in and not in while discussing about different operators in python.

These operators can be used with string operands to check if a sub-string is present within a string.

Let's see the use of these operators with strings in python.

print("d" in "coding") 
True
print('let' not in 'letters')
False

Conversion to String

Python provides two methods to convert different datatypes of python into string, str() and repr() method. Both these functions convert arbitrary python objects into string representation.

str() method simply converts an object to its string representation.
repr() converts an object to its string representation, but returns the object as a string with code that can be re-run to recreate the object.

Difference between str() and repr() can be seen when we use them in print statement.

print(str('look'), repr('look'))
look 'look'

String Formatting in Python

Formatting a string is useful when we want to concatenate or print variables of other type along with strings. In the print() function, we can simply use a comma ( , ) for this purpose as shown in below example :

  1. format()
  2. %
  3. escape sequences
print("Code",125)
Code 125

There are other ways for string formatting, which we are going to learn one by one.

format( ) method

The format() method is used with the print statement to format the format or change the output without using commas. It is placed after the output string and takes variables to be printed as arguments.

We have learnt about this method while discussing about Input and Output in Python.

Let's see some example to refresh the concept.

print("{0} is greater than {1}".format(24,5))
24 is greater than 5
print("Hello {name}, How are you ?".format(name = "Mark"))
Hello Mark, How are you ?

% operator

The % operator is also used to format the string while using the print function.

Let's see how to use it by some examples :

print("Hello student%d, %s percentage is %0.3f"%(3,'your',79.5627))
Hello student3, your percentage is 79.563

Let's observe the above example :

  • %d representation is used for integer values, %s is for strings and %f is for float.
  • You might have noticed that I have written 0.3 before f while using the % operator, it means that decimal part of the floating point number will have only 3 numbers. That is why notice that the actual value 79.5627 is rounded off to 79.563 in the output statement.

Escape Sequences in Python

In a python string, if we want to add a tab, newline or use quotes inside a string then it can be done using escape sequences.An escape sequence is a backslash \ followed by a character.

The following table shows different escape sequences available in python and their meaning.

Escape Sequences Description
\newline Backslash and newline ignored
\\ Backslash ( \ )
\' Single quote ( ' )
\" Double quote ( " )
\a ASCII Bell (BEL)
\b ASCII Backspace (BS)
\f ASCII Formfeed (FF)
\n ASCII Linefeed (LF)
\r ASCII Carriage Return (CR)
\t ASCII Horizontal Tab (TAB)
\v ASCII Vertical Tab (TAB)
\000 Character with octal value 000
\xhh Character with hex value hh

Following are some examples on how to use escape sequences :

str1= 's\np\tam' # here \n and \t are newline and horizontal-tab escape sequences used
print(str1)
s
p   am
str2 = "WELCOME\b\b To Python Learning" #here \b (backspace) escape sequence is used.
print(str2)
WELCOME To Python Learning
str3 = "She said \"How are you ?\"\nHe replied \"I am fine\""  #here \n (newline) and \" (") are escape sequences used.
print(str3)
She said "How are you ?"
He replied "I am fine"
str4 = "\x48\x45\x58 Representation"
print(str4)
HEX Representation

There might be some cases when we would want to ignore the escape sequences within the string. This can be achieved using r or R as prefix to the string.

This means that it is a raw string and escape sequences within the string will be ignored.

Look at the below example to understand this better :

print("\"coding\" is \nfun")
"coding" is 
fun
print(r"\"coding\" is \nfun")
\"coding\" is \nfun

String Methods in Python

In addition to different operations available for strings, python also provides us with multiple built-in methods to manipulate and process strings more easily.

Following is a list of methods provided in python for string manipulation along with small description about each of them.

Method() Description
capitalize() Converts the first character to upper case
casefold() Converts string into lower case
center() Returns a centered string
count() Returns the number of times a specified value occurs in a string
encode() Returns an encoded version of the string
endswith() Returns true if the string ends with the specified value
expandtabs() Sets the tab size of the string
find() Searches the string for a specified value and returns the position of where it was found
format() Formats specified values in a string
format_map() Formats specified values in a string
index() Searches the string for a specified value and returns the position of where it was found
isalnum() Returns True if all characters in the string are alphanumeric
isalpha() Returns True if all characters in the string are in the alphabet
isdecimal() Returns True if all characters in the string are decimals
isdigit() Returns True if all characters in the string are digits
isidentifier() Returns True if the string is an identifier
islower() Returns True if all characters in the string are lower case
isnumeric() Returns True if all characters in the string are numeric
isprintable() Returns True if all characters in the string are printable
isspace() Returns True if all characters in the string are whitespaces
istitle() Returns True if the string follows the rules of a title
isupper() Returns True if all characters in the string are upper case
join() Joins the elements of an iterable to the end of the string
ljust() Returns a left justified version of the string
lower() Converts a string into lower case
lstrip() Returns a left trim version of the string
maketrans() Returns a translation table to be used in translations
partition() Returns a tuple where the string is parted into three parts
replace() Returns a string where a specified value is replaced with a specified value
rfind() Searches the string for a specified value and returns the last position of where it was found
rindex() Searches the string for a specified value and returns the last position of where it was found
rjust() Returns a right justified version of the string
rpartition() Returns a tuple where the string is parted into three parts
rsplit() Splits the string at the specified separator, and returns a list
rstrip() Returns a right trim version of the string
split() Splits the string at the specified separator, and returns a list
splitlines() Splits the string at line breaks and returns a list
startswith() Returns true if the string starts with the specified value
strip() Returns a trimmed version of the string
swapcase() Swaps cases, lower case becomes upper case and vice versa
title() Converts the first character of each word to upper case
translate() Returns a translated string
upper() Converts a string into upper case
zfill() Fills the string with a specified number of 0 values at the beginning

Let's see some of these functions work.

The len() function returns length of a string given as input.

str1 = "Hello"
print(len(str1))
5

The replace() function returns a string where a specified value or Sub-string from string is replaced by another value given to function.

str1 = "Learn Python"
print(str1.replace("e","x"))      
print(str1.replace("Learn","Basic"))
Lxarn Python
Basic Python

The upper() function will return the string in uppercase.
The lower() function will return the string in lowercase.

str1 = "Welcome to Learn Python"

print(str1.upper())
print(str1.lower())
WELCOME TO LEARN PYTHON
welcome to learn python

The strip() function removes white-spaces from start or end of the string and returns the modified string.

str1 = "    Puppies"
print("Without strip : ",str1)
print("With strip() : ",str1.strip())
Without strip :      Puppies
With strip() :  Puppies

The isdigit() function returns True if and only if all characters of the string are numbers.

str1 = "7663"
print(str1.isdigit())
True

It is difficult to understand each and every method for strings. However, we will learn about them as we go ahead with python examples.

Conclusion

In this tutorial, we have discussed about string datatypes in python, accessing and traversing string, different operations on strings and methods for strings provided by python.

If you have any questions please comment below also share your views and suggestions in the comment box.

Leave a Comment