A string is contained within 2 quotes:
In [1]:
"Michael Jackson"
Out[1]:
You can also use single quotes:
In [2]:
'Michael Jackson'
Out[2]:
A string can be spaces and digits:
In [3]:
'1 2 3 4 5 6 '
Out[3]:
A string can also be special characters :
In [4]:
'@#2_#]&*^%$'
Out[4]:
We can print our string using the print statement:
In [5]:
print("hello!")
We can bind or assign a string to another variable:
In [6]:
Name= "Michael Jackson"
Name
Out[6]:
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:
The first index can be accessed as follows:
In [7]:
print(Name[13])
We can access index 6:
In [8]:
print(Name[6])
Moreover, we can access the 13th index:
In [9]:
print(Name[13])
We can also use negative indexing with strings:
The last element is given by the index -1:
In [10]:
print(Name[-1])
The first element can be obtained by index -15:
In [11]:
print(Name[-15])
We can find the number of characters in a string by using 'len', short for length:
In [12]:
len("Michael Jackson")
Out[12]:
We can obtain multiple characters from a string using slicing, we can obtain the 0 to 4th and 8th to the 12th element:
In [13]:
Name[0:4]
Out[13]:
In [14]:
Name[8:12]
Out[14]:
We can also input a stride value as follows, with the '2' indicating that we are selecting every second variable:
In [15]:
Name[::2]
Out[15]:
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]:
In [18]:
Name[0:15:3]
Out[18]:
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]:
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]:
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]:
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" )
Similarly, back slash "t" represents a tab:
In [23]:
print(" Michael Jackson \t 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" )
We can also place an "r" before the string to display the backslash:
In [25]:
print(r" Michael Jackson \ is the best" )
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)
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]:
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".
In [28]:
Name="Michael Jackson"
Name.find('el')
Out[28]:
In [29]:
Name.find('Jack')
Out[29]:
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]:
In [33]:
A="1"
A
Out[33]:
In [34]:
B='2'
B
Out[34]:
In [35]:
C=A+B
C
Out[35]:
In [36]:
D="ABCDEFG"
In [38]:
D[0:3]
Out[38]:
In [39]:
E='clocrkr1e1c1t'
In [40]:
E[0:len(E):2]
Out[40]:
In [43]:
print("\\")
print(r" \ ")
In [45]:
F="You are wrong"
In [46]:
F.upper()
Out[46]:
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]:
In [49]:
G.replace("Mary", "Bob")
Out[49]:
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 [ ]: