Hello friends and Welcome to the tutorial on "Getting started with strings".
At the end of this tutorial, you will be able to, Define strings in different ways. Concatenate strings. Print a string repeatedly. Access individual elements of the string. Learn immutability of strings.
Open the terminal and invoke the ipython interpreter by typing ipython, so type ipython and hit enter
So, what are strings?
In Python anything within either single quotes or double quotes or triple single quotes or triple double quotes are strings.
So if you can type within single quotes This is a string, then in double quotes This is a string too.
Then in triple double quotes This is also a string
Note that it really doesn't matter how many characters are present in the string.
So we can even put a null string or an empty string.
Having more than one control character to define strings is handy when one of the control characters itself is part of the string.
For example you can type in double quotes Python's string manipulation functions are very useful
By having multiple control characters, we avoid the need for escaping characters hyphen hyphen in this case the apostrophe.
Let us now move on to the triple quoted strings.
Let us define multi-line strings without using any escaping.
Everything within the triple quotes is a single string no matter how many lines it extends
So we can type in the terminal, within triple double quotes Having more than one control character to define strings come as very handy when one of the control characters itself is part of the string.
So, we can assign this string to any variable
So lets type a = within single quotes Hello comma World exclamation and hit enter
Now 'a' is a string variable.
String is a collection of characters.
In addition string is an immutable collection which means that the string cannot be modified after it is created.
So all the operations that are applicable to any other immutable collection in Python, works on strings as well.
Hence we can add two strings
So lets type a = within single quotes Hello and hit enter, b = within single quotes World
c = a plus in single quotes comma plus b plus in single quotes exclamation and hit enter then print c
So you will get an output hello comma world
We can add string variables as well as the strings themselves in all the same statement.
The same addition operation performs the concatenation of two strings.
Similarly we can multiply a string with an integer
So lets type a = in single quotes Hello
Type a into 5
So we will get output as hello into 5
It gives another string in which the original string 'Hello' is repeated 5 times.
Now, pause the video here, try out the following exercise and resume the video.
Obtain the string percentage percentage 20 hyphens percentage percentage without typing out all the twenty hyphens.
Let's now look at accessing individual elements of strings.
Since, strings are collections, we can access individual items in the string using the subscripts
So type s = within double quotes percentage percentage plus within double quotes hyphen multiply by 20 plus within double quotes percentage percentage and hit enter
Now to get an output print s
a in square bracket zero gives us the first character in the string.
The indexing starts from 0 for the first character and goes up to (n minus 1) for the last character, where 'n' is the total number of characters in a string.
We can access the strings from the end using negative indices
So type a in square bracket zero and hit enter
a in square bracket minus 1 gives us the last element of the string and a[-2] gives us second element from the end of the string.</nowiki>So type a in square bracket minus 1 and hit enter, then a in square bracket minus 2 and hit enter
Pause the video here, try out the following exercise and resume the video.
Given a string, s = within double quotes Hello World what is the output of
s[ minus 5]
s[ minus 10]
s[minus 15]
Now, s of [-5] gives us 'W'.So in terminal type s of [-5] and hit enter.
s of [-10] gives us 'e' and
So we got an error as hyphen,so lets update the value of s with hello world. So type s = Hello World
Now s of -5 is W. So we got the output as W
So next type s of -10, it gives us e
And s of [-15] gives us an IndexError,as should be expected, since the string given to us is only 11 characters long.
Let us attempt to change one of the characters in a string
So type a = 'hello' and a of [0] = 'H'
As said earlier, strings are immutable.
We cannot manipulate a string.
Although there are some methods which let us manipulate strings, we will look at them in the advanced session on strings.
In addition to the methods that let us manipulate the strings we have methods like split which lets us break the string on the specified separator, the join method which lets us combine the list of strings into a single string based on the specified separator.
Let's revise quickly what we have learnt today.
In this tutorial we have learnt to,
1. Define strings in different ways.
2. Concatenate strings by performing addition.
3.Repeat a string 'n' number of times by doing multiplication.
4. Access individual elements of the string by using their subscripts.
5. And finally use of the concept of immutability of strings.
Here are some self assessment questions for you to solve
1.Write code to assign s, the string ' is called the apostrophe
2. Given strings s and t,s = "Hello" and t = "World" and an integer r, r = 2
What is the output of s multiply r plus s multiply t?
3.How will you change s='hello' to s='Hello'with h in capital
So, s of [0]= H
s of [0]=in single quotes h
Third option is strings are immutable,hence cannot be manipulated
Let's look at the answers,
1. The given string can be assigned in this manner
So type s = in double quotes` is called the apostrophe
2. The operation s into r plus s into t will print each of the two words twice Hello Hello World World
Strings are immutable.
Therefore they cannot be manipulated.
Hope you have enjoyed this tutorial and found it useful.
Thank you!