Hello guys. Welcome to LearnVern. In the last topic, we saw a Control Looping Statement.
Today we will work on String. We will see how String works & how we can use it under Python.
First of all, what is String?
String is a datatype under Python, which allows the value of multiple data types to be stored inside it.
Meaning you can store an integer in it, you can store characters as well, you can store double & float value also in String.
String is usually written with single or double quotation marks.
It is a collection of characters.
What can be done with String?
We will see how to create String in multiple ways, indexing & slicing and how to apply builtin functions. We are going to see all of that.
Finally moving onto practicals where we will see how to use String in better ways.
Jupyter has opened up.
I’ll markdown first.
How to use String in Python. Forgot to add a hash here.
I’ll open a new tab & write down a comment.
What will be the comment? The comment will be how to use Single quotes String here.
Which string? How to use Single quotes String here in Python.
I’ll write down Single Quote in String.
I’ll print now. You can now write in a single quote string as well.
See we took a single quote here & wrote a string hello world inside it and when we gave it to print, it printed it.
This is possible only & only under Python.
Now suppose I’ll use Double Quote string here.
Let’s take a print. We will use Double Quotes in print & write Hello World.
It will allow us to use String here as well.
Here we saw how to apply String with Single & Double quotes.
Now suppose I want to write a Multiline string.
So let’s see how to write multiline strings.
First of all, I’ll select Markdown from here.
Multiline String.
How is Multiline String written?
Let’s say I have a variable A.
If you want to work with Multiline, you’ll have to work with triple quotation marks.
Remember, triple quotation marks.
You’ll write multiline strings within it.
Suppose I wrote that Python is booming in the market.
Next line. Python is a smart language.
Python is used in AI, machine learning and data science.
I wrote 3 lines here...if you want multiline…
You saw we wrote 1st line, then 2nd line & then 3rd line.
Now when I print A, it did print the Multiline String.
We saw how to use Multiline strings in Python.
I hope you understood the whole concept of how to use Single Quote, double quote as well as with Multiline strings.
Now after using the Multiline string, we will update in String.
Let’s see how we can update a string.
I shall write Update String here.
I have a variable here A inside which the value is Hello World and an exclamation mark.
Now I’ll simply print A. Run & it will print out Hello World.
But now I want to print Hello Python instead of Hello World.
Let’s see how to do it.
I’ll write Print here.
I’ll simply write Update String to know how the update works.
Then write down the variable.
Now we will use Indexing. I’ll show you how Indexing works right after this.
Hello has 5 letters, then space and now I want to write Python instead of World.
So we have to display it at 6th position.
And I’ll add a + sign and print Python here.
I wrote Python after + as I want to update it instead of World.
Now we will run it to confirm.
So what is our updated string?Hello Python.
First it was Hello World and now it is Hello Python.
So this is how you can update a particular string as well.
Now we will move ahead to see how String Slicing works.’
It is also known as String using Array.
What is it called? String using Array.
Let’s see.
I’ll write String Slicing here.
First of all I’ll take a string here Hello Python.
We’ll take a little bit longer string so it can be more fun to slice it, okay.
First of all, we have to access the value on the particular index.
I’ll write down Access the value of Index 1.
I’ll use print here. A.
We’ll write a message first of all.
Index 1 and A brackets.
You have to use square brackets here.
Why? Because we have to use Array here.
We have to slice the string. Meaning we are going to cut it into pieces.
I want the value of the 1st index so I’ll write 1 here in the square.
We have 1st index E. The H of Hello is at 0 and E is at 1st position.
Why? Because Index always starts with 0.
In the same way, let’s understand Access the value between index 1 to 6.
Access the value between index 1 to 6.
We will take the print Index between 1 to 6.
So we can know that our string is stored under A.
Now we have to bring out all the characters between 1 to 6 strings.
So 1:6. Why did we write 1:6? 1 is the starting point & 6 is our ending point.
Let’s see.
You can see those specific characters are shown.
So this is how you can do 1 to 6 indexing slices in a list.
Let’s try something else now.
Access the value till index 6.
We had used between before.
Here we want to print all the values from 0 to 6.
I’ll take print here. I’ll write Index till 6.
And take A. We have to go till 6 so we don’t need to take a starting point.
Only have to write 6. When you press Enter, you can see the whole Hello here.
Clear?
It even calculated the space. Now if I write 7, then it’ll bring P.
You can do index slicing till 6.
Now I have to access the last value from the string.
I want to access the last value which is N.
I want to access Python’s N.
How will I do it?
Let’s see.
I wrote a message about Last Value.
I want to access the last value so I’ll just write -1.
It showed us last value N.
So this is how you can work under String Slicing.
How you cut the string, how you use it with Array…
Meaning whatever indexing value we wrote was an Array in a way.
Okay? Clear?
So this is how you do String Slicing in Python.
Next up we will see the inbuilt method of string.
How to use the inbuilt methods.
String Inbuilt methods.
Our first method is Capitalise. How is the Capitalise method used?
I’ll write Capitalise first of all.
So what does this method do?
This method converts the first character into a string.
Let’s see how it works.
I took variable A. Wrote Hello and Welcome to LearnVern.
Now you can see that our Hello’s H is in small letters.
Now how to use the Capitalise method.
A . (dot) you can see as you typed, capitalise was prompted.
Now when I print X...right now, hello’s H is small, right?
Now when we run it, it gets converted into capital H.
Inshort, it converts our first letter, first character to Capital.
So the name of the method itself says it all about the concept of the method.
This is how the Capitalise method is used.
Moving onto the next one that is the Casefold Method.
Which method? Casefold method.
This method converts all the small letters into capital.
Let’s see.
A = I wrote Hello.
Wait, sorry guys. Here it will be Capital into Small.
Sorry guys. Here it will be Capital into Small okay.
Hello and Welcome to LearnVern.
We’ve kept every first letter of every word capital.
Now I’ll take a variable X. A casefold. That is our method.
Now it’s time to Print.
As you can see, our first capital letters got converted into a small letter.
There is another method which is the Center Method.
Which method? Center method.
This is used to bring your string into center.
This method brings your string into the center but within the range we’ve entered.
Let’s see how.
Suppose I wrote A = Python.
Now I’ll simply print A.
You can see that it got printed on the left hand side.
Now we will update it a bit.
Let’s see how to do that.
X = A . (dot) Center method.
Suppose we gave a space of 20.
We need space of 20 to bring it to the center.
Now I’ll Print X.
You can see that the Python above is on the left hand side.
But the second one is the updated one.
I’ll add a message here Update using Center Method.
You can see that updated X got printed in the center.
It left the space of 20 & printed Python here.
So this is how the Center method works under String.
There are several methods here...okay I’ll show you one more.
We will see one method named Count method.
Which method? Count.
So this method is used to count the repeated words in String.
It will count all the words which repeat in a string.
I’ll write here - I love Python, Python is a smart language.
I’ve written Python 2 times here & I want to count it.
A . (dot) Count method.
We’ll have to say what we want to count here.
I wrote Python here & then wrote Print.
Print what? Print X. Let’s see.
It showed us that the word Python is used 2 times in the string.
So this is how the inbuilt methods work under Python. -
Now we will see how to compare a String.
When you want to compare strings, you have to use the Comparison operators.
I’ve already told you about this operator during the Operators video.
If you don’t know, I request you to watch the Operators video first and then continue this video.
So what is String Comparison?
It is very simple & easy.
It helps in comparison of 2 strings.
Suppose I have a string Str1 in which I wrote Learnvern.
And I have another string Str 2 and I wrote Learnvern in the same exact manner as above.
Now I have to compare both the strings.
We are comparing both the spellings & case sensitivity.
So how will we do it?
It’s very simple. I’ll write result here.
Then write Str1 == Str2.
Now when I print the result, we will get True when we run it.
Why will it be true? It will be true because both the spellings in String 1 and String 2, both are same & in same case as well.
String Comparison follows the English Dictionary.
How? See..
Suppose I took Str1 here = Learnvern. Okay.
Then I took Str2 and wrote Python here.
So Str 1 & Str 2 both have different values.
Now we will compare both which is String 1 == string 2.
Now when we print the result, it will say False.
Why False? Because both the spellings are different, both the strings are different.
So this is one type of String Comparison.
I’ll give you one more example to make you understand clearly.
Str 1 = A. Str 2 = B.
Now I want to check the result that String 1 < String 2.
Now printing the result.
It is showing me True. Why True?
If you follow the English Dictionary, first comes A and then comes B.
And then comes C.
I’ll take one more string here Str3 = C.
And I’ll change here to Str 2 < Str 3.
It will pass True again after I run it because B is smaller than C.
If you follow the English Dictionary, first comes B and then comes C.
Inshort, the alphabet which comes first is smaller than the next one.
So this is how String comparison takes place.
Let’s say I write Str2 > Str 3.
It will show False.
Why? Because C comes after B which means C is greater than B.
This is how String comparison takes place according to the English Dictionary.
So this was String Comparison.
Our next topic will be List.
Share a personalized message with your friends.