Skip to content
Home » STRING MANIPULATION

STRING MANIPULATION

INTRODUCTION

You know that python strings are characters enclosed in quotes of any type- single quotation marks, double quotation marks and triple quotation marks. You have also learnt things like- an empty string is a string that has 0 characters (i.e., it is just a pair of quotation marks) and the Python strings are immutable.

TRAVERSING A STRING

You know that individual characters of a string are accessible through the unique index of each character. Using the indexes, you can traverse a string character by character. Traversing refers to iterating through the elements of a string, one character at a time.

To traverse through a string, you can write a loop like:

name= “superb”
for i in name:
    print(i,’-‘,end=’ ‘)

The above code will print

s-u-p-e-r-b-

STRING OPERATORS

In this section you will be learning to work with various operators that can be used to manipulate strings in multiple ways. We’ll be talking about basic operators + and *, membership operators in and not in and comparison operators for strings.

BASIC OPERATORS

The two basic operators of strings are: + and *. You have used these operators as arithmetic operators before addition and multiplication respectively. But when used with strings, + operator performs concatenation rather than addition and * operator performs replication rather than multiplication.

Strings are immutable. Thus every time you perform something on a string that changes it, Python will internally create a new string rather than modifying the old string in place.

String Concatenation Operator +

The + operator creates a new string by joining the two operand strings, e.g.,

“tea”+”pot”

Will result into

‘teapot’

Consider some more examples:

Expression                              will result into

‘1’+’1’                                                  ‘11’

‘a’+’0’                                                  ‘a0’

‘1817’+’tv’                                         ‘1817tv’

Caution!!!

Another important thing that you need to know about + operator is that this operator can work with numbers and strings separately for addition and concatenation respectively, but in the same expression, you cannot combine numbers and strings as operands with a + operator.

String Replication Operator *

The * operator when used with numbers (i.e., when both the operands are numbers), it performs multiplication and returns the product of the two number operands.

To use a * operator with strings you need two types of operands- a string and a number, i.e., as number * string or string * number

Where string operand tells the string to be replicated and number operand tells the number of times, it is to be repeated; Python will create a new string that is a number of repetitions of the string operand. For example:

            3*’go!’

Will return

‘go!go!go!’

Caution!!!

Another important thing that you need to know about * operator is that this operator can work with numbers as both operands for multiplication and with a string and a number for replication respectively, but in the same expression, you cannot have strings as both the operands with a * operator.

MEMBERSHIP OPERATORS

There are two membership operators for strings (in fact for all sequence types). These are in and not in.

in         returns True if a character or a substring exists in the given string; False otherwise.

not in   returns True if a character or a substring does not exist  in the given string; False otherwise.

Both membership operators (when used with strings), require that both operands used with them are of string type, i.e.,

            <string> in <string>

            <string> not in <string>

COMPARISION OPERATORS

Python’s standard comparison operators i.e., all relational operators (<, <=, >, >=, ==, !=) apply to strings also. The comparisons using these operators are based on the standard character by character comparison rules for Unicode (i.e., dictionary order)

‘a’==’a’                                    will give  True

“abc”==”ABC”                         will give  False

‘a’!=’A’                         will give  True

Equality and non-equality in strings are easier to determine because it goes for exact character matching for individual letters including the case.

Internally Python compares using Unicode values (called ordinal value), let us know about most common characters and their ordinal values. For most common characters, the ASCII values and Unicode values are the same.

            Characters                                          Ordinal values

            ‘0’ to ‘9’                                                     48 to 57

              ‘A’ to ‘Z’                                                    65 to 90

            ‘a’ to ‘z’                                                      97 to 122

‘a’ < ‘A’                        will give False             because the Unicode value of lower case letters is higher than the upper case letters; hence ‘a’ is greater than ‘A’, not lesser.

DETERMINING ORDINAL/ UNICODE VALUE OF A SINGLE CHARACTER

Python offers a built in function ord() that takes a single character and returns the corresponding ordinal Unicode value. It is used as per following format:

            ord(<single-character>)

But you need to keep in mind that ord() function requires single character string only.

The opposite of ord() function is chr(), i.e., while ord() returns the ordinal value of a character, the chr() takes the ordinal value in integer form and returns the character corresponding to that ordinal value. The general syntax of chr() function is:

chr(<int>)

STRING SLICES

In python, the term ‘string slice’ refers to a part of the string, where strings are sliced using a range of indices.

That is, for a string say name, if we give name[n:m] where n and m are integers and legal indices, Python will return a slice of the string by returning the characters falling between indices n and m- starting at n, n+1, n+2,…… till n-1. Let us understand this with an examples.

word= ‘amazing’

Then,

 word[0:7]       will give           ‘amazing’

word[0:3]        will give           ‘ama’

word[2:5]        will give           ‘azi’

word[-7:-3]      will give           ‘amaz’

word[:7]          will give           ‘amazing’ (missing colon before colon is taken as 0)

word[3:]          will give           ‘zing’ (missing colon after colon is taken as the length of the string)

Interesting interface:        

For any index n, s[:n]+s[n:] will give the original string s.

You can give a third (optional) index (say n) in string slice too. With that every nth element will be taken as part of slice.

word[1:6:2]        will give             ‘mzn’          2 is called the step value. It will take every 2nd character starting from index 1 till index<6.

word[-7:-3:3]     will give             ‘az’              it will take every 3rd character starting with index -7 to index -3.

word[: :-1]          will give             ‘gnizama’   every character is taken backward

Another interesting interference is: Index out of bounds causes error with strings but slicing a string outside the bounds does not cause error.           

ABOUT FORMAT METHOD:

format() takes the arguments, formates them and places them in the string where the placeholders{} are

EXAMPLE:

age = 36
txt = "My name is John, and I am {}"
print(txt.format(age))

OUTPUT:

My name is John, and I am 36

this method takes unlimited of aurguments and placed into respective place holders:

EXAMPLE:

quantity = 3
itemno = 567
price = 49.95
myorder = "I want {} pieces of item {} for {} dollars."
print(myorder.format(quantity, itemno, price))

OUTPUT:

I want 3 pieces of item 567 for 49.95 dollars.

we can use index numbers to sure the arguments are placed in correct place holders:

EXAMPLE:

quantity = 3
itemno = 567
price = 49.95
myorder = "I want to pay {2} dollars for {0} pieces of item {1}."
print(myorder.format(quantity, itemno, price))

OUTPUT:

I want to pay 49.95 dollars for 3 pieces of item 567

ESCAPE CHARACTER

It is used to insert characters that are illegal in a string

An escape character is \ followed by the character we want to insert

The escape character allows you to use double quotes when you normally would not be allowed:

txt = “We are the so-called \”Vikings\” from the north.”

STRING METHODS

-They donot change the original string

str.capitalize()–>converts first character to upper case

EXAMPLE:

print(("i love India").capitalize())

OUTPUT:

I love india

str.center(#some number)–>returns a centered string

EXAMPLE:

v="VEEKSHITHA"
print(v.center(59))

OUTPUT:

                         VEEKSHITHA 

str.count(value to be counted)–>returns the number of times a specifies value occurs in a string

EXAMPLE:

v="VEEKSHITHA"
print(v.count('E'))

OUTPUT:

2

str.endswith(value)–>returns true if the string ends with the specified value

EXAMPLE 1:

s="sushanth"
print(s.endswith('th'))

OUTPUT:

True

EXAMPLE 2:

s="sushanth"
print(s.endswith('thS'))

OUTPUT:

False

str.find(sub,start,end)–>returns lowest index in the string where the substring is found.Returns -1 if sub is not found

EXAMPLE 1:

str='  ringa ringa roses'
sub='ringa'
print(str.find(sub))

OUTPUT:

2

EXAMPLE 2:

str='  ringa ringa roses'
sub='ringa'
print(str.find(sub,13,25))

OUTPUT:

-1

str.index()–>returns lowest index in the string where the substring is found.if the substring is not found then a value error is raised

EXAMPLE 1:

str='abababcabcdabcde'
print(str.index('abc'))

OUTPUT:

4

EXAMPLE 2:

str='abababcabcdabcde'
print(str.index('abc',5,9))

OUTPUT:

ValueError                                Traceback (most recent call last)

Cell In[8], line 2

      1 str=’abababcabcdabcde’

—-> 2 print(str.index(‘abc’,5,9))

ValueError: substring not found

str.isalnum()–>Returns True if all characters in the string are alphanumeric

EXAMPLE 1:

str="123abc"
print(str.isalnum())

OUTPUT:

True

EXAMPLE 2:

str="abc"
print(str.isalnum())

OUTPUT:

True

str.isalpha()–>Returns True if all characters in the string are in the alphabet

EXAMPLE 1:

str="abc"
print(str.isalpha())

OUTPUT:

True

EXAMPLE 2:

str="abc  "
print(str.isalpha())

OUTPUT:

False

str.isdigit()–>Returns True if all characters in the string are digits

EXAMPLE 1:

str="123  "
print(str.isdigit())

OUTPUT:

False

EXAMPLE 2:

str="123 "
print(str.isdigit())

OUTPUT:

True

str.islower()–>Returns True if all characters in the string are lower case

EXAMPLE:

str="computer"
print(str.islower())

OUTPUT:

True

str.isspace()–>Returns True if all characters in the string are whitespaces

EXAMPLE:

str=" "
print(str.isspace())

OUTPUT:

True

str.istitle()–>Returns True if all words starts with uppercase characters and all the remaining letters are lowercase

EXAMPLE:

str="I Love Python"
print(str.istitle())

OUTPUT:

True

str.isupper()–>Returns True if all characters in the string are upper case

EXAMPLE 1:

str="Joining"
print(str.isupper())

OUTPUT:

False

EXAMPLE 2:

str="JOINING"
print(str.isupper())

OUTPUT:

True

str.join()–>Joins the elements of an iterable to the end of the string

EXAMPLE:

print('@'.join("Hello"))

OUTPUT:

H@e@l@l@o

str.lower()–>Converts a string into lower case

EXAMPLE:

str='Omega'
print(str.lower())

OUTPUT:

omega

str.lstrip()–>Returns a left trim version of the string

EXAMPLE:

str='   computer notebook   '
print(str.lstrip())

OUTPUT:

computer notebook

str.partition()–>Returns a tuple where the string is parted into three parts

EXAMPLE:

txt="I enjoy working in python"
x=txt.partition('working')
print(x)

OUTPUT:

(‘I enjoy ‘, ‘working’, ‘ in python’)

str.replace(old,new)–>Returns a string where a specified value is replaced with a specified value

EXAMPLE:

str='I work for u'
print(str.replace('work','care'))

OUTPUT

I care for u

str.rstrip()–>Returns a right trim version of the string

EXAMPLE:

str='   computer notebook   '
print(str.rstrip())

OUTPUT:

computer notebook  

str.split()–>Splits the string at the specified separator, and returns a list

EXAMPLE:

s="I love python"
print(s.split())
print(s.split(" "))
print(s.split('o'))

OUTPUT:

[‘I’, ‘love’, ‘python’]

[‘I’, ‘love’, ‘python’]

[‘I l’, ‘ve pyth’, ‘n’]

str.startswith()–>Returns true if the string starts with the specified value

EXAMPLE:

str='abacd'
print(str.startswith('ab'))

OUTPUT:

True

str.strip()–>Returns a trimmed version of the string

EXAMPLE:

str='      computer noteboook    '
print(str.strip())

OUTPUT:

computer notebook

str.swapcase()–>Swaps cases, lower case becomes upper case and vice versa

EXAMPLE:

str="String"
print(str.swapcase())

OUTPUT:

sTRING

str.title()–>Converts the first character of each word to upper case

EXAMPLE:

str="i love python"
print(str.title())

OUTPUT:

I Love Python

str.upper()–>Converts a string into upper case

EXAMPLE:

s="Python"
print(s.upper())

OUTPUT:

PYTHON