PYTHON STRING
3rd Part
Today our string chapter last part. Let’s get coding:-
Suppose, you write a string. Let’s write:
>>>str='bcd '
>>> str
'bcd '
str we use extra space. Now you want to remove extra space. How you
remove extra space? We have to use the strip() method. strip() method
remove extra space.
>>> str.strip()
'bcd'
But there is an important point. When we use ‘.strip()’ method it
actually, makes a new string. It does not change the original str.
Now if you want to use new string, then if you have to assign your
new string new variable.
>>> str
>>> str2 = str.strip()
>>> str2
'bcd'
now str2 have no space. Complete new variable.
>>> str = ' a b c d '
>>> str
'a b c d '
Now at first remove first and last space.
>>> str2=str.strip()
>>> str2
‘a b c d’
we don’t change inside a string. Now, Let’s see another example.
>>> str='AbcDeF'
>>> str.upper()
'ABCDEF'
>>> str.lower()
'abcdef'
At first, we use ‘.upper()’ method. This method turn all character
capital. If you want to show all small letter then use lower()
method. But most important str doesn’t change. why every time we use
variable?. Because the string is non-mutable or immutable object.
TIP: You can’t change the Python string.
How to write a reverse string?
>>> school='Newyork'
>>> "".join(reversed(school))
'kroyweN'
That’s it. So, We finish our string class. Now, go to back to your
room and just relax and watch movie and song.Do whatever you like to
do. But when start string goes back to the first part and read
again.Write lot’s code and you became a ninja coder.
Best of Luck.
No comments:
Post a Comment