Python fragmentation knowledge (1): How to Use strip lstrip rstrip

Source: Internet
Author: User

I. Principles:

Strip in Python is used to remove the first and last characters of a string. Similarly, lstrip is used to remove the characters on the left, and rstrip is used to remove the characters on the right. All three functions can be passed in a parameter, specifying the first and last characters to be removed. It should be noted that the input is a character array. the compiler removes all the corresponding characters at both ends until no matching characters exist, such as: theString = 'saaaay yes no yaaaass 'print theString. strip ('put') theString is removed from the ['s ', 'A', 'y'] array until the characters are not in the array. Therefore, the output result is: yes no. The principle of lstrip and rstrip is the same. Note: When no parameters are input, spaces at the beginning and end are removed by default. TheString = 'saaaay yes no yaaaass 'print theString. strip ('put') print theString. strip ('put') # There is a space after 'put' print theString. lstrip ('put') print theString. rstrip ('put') running result: yes no es no yes no yaaaass saaaay yes no
Note: This explanation comes from :( pylemon's notebook): http://www.cnblogs.com/pylemon/archive/2011/05/18/2050179.html

Ii. Practical Application

Here is a demo used to control the output menu in pythonIED, select the corresponding options, and perform the corresponding operations. As follows:
Suppose there are two functions in the program: newUser and oldUser.

1 # Menu Control Interface 2 def showmenu (): 3 prompt = '''4*************************** 5 Welcome to Python System! 6 ----------------------- 7 | (N) ew User Login | 8 | (L) ogin your system | 9 | (Q) uit | 10 ----------------------- 11 Enter Choice: 12 ************************* 13 ''' 14 done = False15 while not done: 16 chosen = False17 while not chosen: 18 try: 19 choice = raw_input (prompt ). strip () [0]. lower () # Take the first character of the input string 20 Tb (EOFError, KeyboardInterrupt): 21 choice = 'q' # throw an exception 22 print '\ n You picked: [% s] '% choice23 if choice not in 'nlq': # determine whether the first character in the output string belongs to 'nlq' 24 print 'invalid option, tyr again '25 else: 26 chosen = True # Jump out of the loop if everything is normal 27 # control based on input information 28 if choice = 'q': done = True # if 'q' is input, the entire loop is exceeded, 29 if choice = 'N': newUser () # if 'n' is input, call the user registration function 30 if choice = 'l': oldUser () # if 'L' is entered, the user login function 31 32 is called. # main program 33 if _ name __= = "_ main _": 34 showmenu ()

Note: Pay attention to the red part of the code.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.