String Operations


Table of Contents


Strings

A string is contained within 2 quotes:


In [1]:
"Michael Jackson"


Out[1]:
'Michael Jackson'

You can also use single quotes:


In [2]:
'Michael Jackson'


Out[2]:
'Michael Jackson'

A string can be spaces and digits:


In [3]:
'1 2 3 4 5 6 '


Out[3]:
'1 2 3 4 5 6 '

A string can also be special characters :


In [4]:
'@#2_#]&*^%$'


Out[4]:
'@#2_#]&*^%$'

We can print our string using the print statement:


In [5]:
print("hello!")


hello!

We can bind or assign a string to another variable:


In [6]:
Name= "Michael Jackson"
Name


Out[6]:
'Michael Jackson'

Indexing

It is helpful to think of a string as an ordered sequence. Each element in the sequence can be accessed using an index represented by the array of numbers:

</a>

The first index can be accessed as follows:


In [7]:
print(Name[13])


o

We can access index 6:


In [8]:
print(Name[6])


l

Moreover, we can access the 13th index:


In [9]:
print(Name[13])


o

We can also use negative indexing with strings:

</a>

The last element is given by the index -1:


In [10]:
print(Name[-1])


n

The first element can be obtained by index -15:


In [11]:
print(Name[-15])


M

We can find the number of characters in a string by using 'len', short for length:


In [12]:
len("Michael Jackson")


Out[12]:
15

We can obtain multiple characters from a string using slicing, we can obtain the 0 to 4th and 8th to the 12th element:

</a>


In [13]:
Name[0:4]


Out[13]:
'Mich'

In [14]:
Name[8:12]


Out[14]:
'Jack'

We can also input a stride value as follows, with the '2' indicating that we are selecting every second variable:

</a>


In [15]:
Name[::2]


Out[15]:
'McalJcsn'

We can also incorporate slicing with the stride. In this case, we select the first five elements and then use the stride:


In [16]:
Name[0:5:2]


Out[16]:
'Mca'

In [18]:
Name[0:15:3]


Out[18]:
'Mhlas'

We can concatenate or combine strings by using the addition symbols, and the result is a new string that is a combination of both:


In [19]:
Statement = Name + "is the best"
Statement


Out[19]:
'Michael Jacksonis the best'

To replicate values of a string we simply multiply the string by the number of times we would like to replicate it. In this case, the number is three. The result is a new string, and this new string consists of three copies of the original string:


In [20]:
3*"Michael Jackson "


Out[20]:
'Michael Jackson Michael Jackson Michael Jackson '

You can create a new string by setting it to the original variable. Concatenated with a new string, the result is a new string that changes from Michael Jackson to “Michael Jackson is the best".


In [21]:
Name= "Michael Jackson"
Name= Name+" is the best"
Name


Out[21]:
'Michael Jackson is the best'

Escape Sequences

Back slashes represent the beginning of escape sequences. Escape sequences represent strings that may be difficult to input. For example, back slash "n" represents a new line. The output is given by a new line after the back slash "n” is encountered:


In [22]:
print(" Michael Jackson \n is the best" )


 Michael Jackson 
 is the best

Similarly, back slash "t" represents a tab:


In [23]:
print(" Michael Jackson \t is the best" )


 Michael Jackson 	 is the best

If you want to place a back slash in your string, use a double back slash:


In [24]:
print(" Michael Jackson \\ is the best" )


 Michael Jackson \ is the best

We can also place an "r" before the string to display the backslash:


In [25]:
print(r" Michael Jackson \ is the best" )


 Michael Jackson \ is the best

String Operations

There are many string operation methods in Python that can be used to manipulate the data. We are going to use some basic string operations on the data.

Let's try with the method "upper"; this method converts lower case characters to upper case characters:


In [26]:
A="Thriller is the sixth studio album"
print("before upper:",A)
B=A.upper()
print("After upper:",B)


before upper: Thriller is the sixth studio album
After upper: THRILLER IS THE SIXTH STUDIO ALBUM


The method replaces a segment of the string, i.e. a substring with a new string. We input the part of the string we would like to change. The second argument is what we would like to exchange the segment with, and the result is a new string with the segment changed:


In [27]:
A="Michael Jackson is the best"
B=A.replace('Michael', 'Janet')
B


Out[27]:
'Janet Jackson is the best'

The method "find" finds a sub-string. The argument is the substring you would like to find, and the output is the first index of the sequence. We can find the sub-string "jack" or "el".

</a>


In [28]:
Name="Michael Jackson"
Name.find('el')


Out[28]:
5

In [29]:
Name.find('Jack')


Out[29]:
8

If the sub-string is not in the string then the output is a negative one. For example, the string 'Jasdfasdasdf' is not a substring:


In [30]:
Name.find('Jasdfasdasdf')


Out[30]:
-1

Quiz on Strings

What is the value of the variable "A" after the following code is executed?

A="1"


In [33]:
A="1"
A


Out[33]:
'1'

What is the value of the variable "B" after the following code is executed?

B="2"


In [34]:
B='2'
B


Out[34]:
'2'

What is the value of the variable "C" after the following code is executed?

C=A+B


In [35]:
C=A+B
C


Out[35]:
'12'

Consider the variable "D": use slicing to print out the first three elements:


In [36]:
D="ABCDEFG"

In [38]:
D[0:3]


Out[38]:
'ABC'
``` "print(D[:3]) or print(D[0:3])" ```

Use a stride value of 2 to print out every second character of the string "E":


In [39]:
E='clocrkr1e1c1t'

In [40]:
E[0:len(E):2]


Out[40]:
'correct'
``` "print(E[::2])" ```

In [43]:
print("\\")
print(r" \ ")


\
 \ 
``` print(" \\" ) or print(r" \ " ) ```

Convert the variable "F" to uppercase:


In [45]:
F="You are wrong"

In [46]:
F.upper()


Out[46]:
'YOU ARE WRONG'
``` F.upper() ```

Consider the variable "G", and find the first index of the sub-string 'snow':


In [47]:
G="Mary had a little lamb Little lamb, little lamb Mary had a little lamb \
Its fleece was white as snow And everywhere that Mary went Mary went, Mary went \
Everywhere that Mary went The lamb was sure to go"

In [48]:
G.find("snow")


Out[48]:
95
``` G.find('snow') ```

In the variable "G", replace the sub-string Mary with "Bob":


In [49]:
G.replace("Mary", "Bob")


Out[49]:
'Bob had a little lamb Little lamb, little lamb Bob had a little lamb Its fleece was white as snow And everywhere that Bob went Bob went, Bob went Everywhere that Bob went The lamb was sure to go'
``` G.replace("Mary","Bob") ```

About the Authors:

Joseph Santarcangelo has a PhD in Electrical Engineering, his research focused on using machine learning, signal processing, and computer vision to determine how videos impact human cognition. Joseph has been working for IBM since he completed his PhD.


In [ ]: