Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
July 29, 2021 07:35 am GMT

Learning Python-Basic course: Day 24, String Methods Part-3

Today we will finally finish up with string methods. In case you missed the previous part 1 and part 2 of string methods, then you can check them out.

String manipulation in Python can be implemented in a very easy way with the use of inbuilt, methods. This comes very handy when we need to manipulate strings while working with complex operations. For example, this saves a lot of code while working with 'front-endish' applications like GUI. Let's say we are writing a password management system and want to check if only alphanumeric vakyes are present. In such cases, the inbuilt functions save code wile compared to long nested for loops.

Another feature of string slicing can be very handy and save a lot of code, say when you want to reverse strings or take only a part of whole sentences.

In this part, I am presenting only sample programs, as the methods do not require much explaination and code is commented wherever necessary.

Trimming Strings

We will now use inbuilt functions to trim strings into parts.

print("
1. left justified")str ="Python".ljust(10,".")print(str)print("
2. Right justified")str ="Python".rjust(10,".")print(str)print("
3. Center Aligned")str ="Python".center(10,".")print(str)#lstrip() Returns a left trim version of the string#rstrip() Returns a right trim version of the string#strip() Returns a left and right trim version of the string

output-

 1. left justifiedPython.... 2. Right justified....Python 3. Center Aligned..Python..

Stripping Strings

print("
****** left strip()*****
")st=" Python Level 2 "print(st)print("length = ", len(st))lst =st.lstrip() #trimprint(lst)print("length = ", len(lst))print(lst, "after the left strip method")print("
****** right strip()*****
")rst =st.rstrip() #trimprint("length = ", len(rst))print(rst, "after the right strip method")print("
****** strip()*****
")sst =st.strip() #trimprint("length = ", len(sst))print(sst, "after the strip method")

OUTPUT

****** left strip()*****   Python Level 2    length =  21Python Level 2    length =  18Python Level 2     after the left strip method****** right strip()*****length =  17   Python Level 2 after the right strip method****** strip()*****length =  14Python Level 2 after the strip method

Slicing Strings

print("
****** slicing of strings *****
")st = "Python Fundamentals"print(st, "Length = ", len(st))s1= st[2:8]print("st[2:8]".ljust(20), s1)s1= st[0:len(st)]print("st[0:len(st)]".ljust(20), s1)s1= st[0:4]print("st[0:4]".ljust(20), s1)s1= st[4:len(st)] print("st[4:len(st)]".ljust(20), s1)s1= st[3:-5] print("st[3:-5]".ljust(20), s1)s1= st[4:0] print("st[4:0]".ljust(20), s1)s1= st[4:] print("st[4:]".ljust(20), s1)s1= st[8:-1]print("st[8:-1]".ljust(20), s1)s1= st[8:-3]print("st[8:-3]".ljust(20), s1)

OUTPUT

****** slicing of strings *****Python Fundamentals         Length =  19st[2:8]              thon Fst[0:len(st)]        Python Fundamentalsst[0:4]              Pythst[4:len(st)]        on Fundamentalsst[3:-5]             hon Fundamest[4:0]              st[4:]               on Fundamentalsst[8:-1]             undamentalst[8:-3]             undament

Reversing of strings

st="Learning Python Course"# :: and -ve index to reverse the strings1= st[4::-1]print("st[4::-1]".ljust(20), s1)s1= st[8::-2]print("st[8::-2]".ljust(20), s1)s1= st[::-1]print("st[::-1]".ljust(20), s1)s1= st[::-2]print("st[::-2]".ljust(20), s1)s1= st[1::-1]print("st[1::-1]".ljust(20), s1)s1= st[5:10][::-1]print("st[5:10][::-1]".ljust(20), s1)s1= st[4::3]print("st[4::3]".ljust(20), s1)

OUTPUT

st[4::-1]            nraeLst[8::-2]             nnaLst[::-1]             esruoC nohtyP gninraeLst[::-2]             ero otPgirest[1::-1]            eLst[5:10][::-1]       P gnist[4::3]             ngyoCr

So friends that's all for this part. Hope you all are enjoying. Please let me know in the comment section if you liked it or not. And don't forget to like the post if you did. I am open to any suggestions or doubts. Just post in the comments below or gmail me.
Thank you for being so patient.


Original Link: https://dev.to/aatmaj/learning-python-basic-course-day-24-string-methods-part-3-1mg9

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To