2018-5-27-python Full Stack development day11-string

Source: Internet
Author: User
Tags string methods

Summary of String methods

1. Methods that must be mastered

1.1 Join ()

Stitching elements in a string according to a specific delimiter

  

test=' who are you? 'print(test) v='* *'. Jion ( Test)print(v)
who are you, who are you * * * * * *

The format is special, first of all the characters need to be inserted into the front, in addition to not only the string, but also the dictionary, list, and so on into the string. Put the string behind Jion

1.2split (", 2)

Split the string, the first argument is what the benchmark, the second parameter is split several times, but after the split, the first parameter disappears

  

test='alexalexalex'v=test.split ('l', 2) Print (v)

Output Result:

['a''exa'exalex' ]

Split with L as parameter, and divide two times, the output result does not retain L

1.3 Find to find a string

Find (' l ', 5,100)

Search for the string to find the first position in the interval.

  

test='alexalexalexalex'v=test.find ('l', 2,10  )Print(v)

1.4 Strip

1.4.1 Strip can remove spaces on both sides of a string

  

test='    Alex    'v=test.strip (test)print (v)
------
Alex

1.4.2 Strip can remove certain characters

    

test='Alex'v=test.strip ('A')Print (v)

Output: Lex

1.5 Upper ()

Convert lowercase letters to uppercase

  

test='Alex'v=test.upper ()print(v)

Output: ALEX

1.6 Lower ()

Convert uppercase letters to lowercase

  

test='ALEX'v=test.lower ()print(v)

Output: Alex

2. Special usage

2.1 Gets a value from a string according to the index

  

test='Alex'v=test[0]print(v)--------  A

2.2 Gets a value of a string based on the range

  

test='Alex'v=test[0:3]print(v)------Ale 

The value that you want to get is determined by the range, which is the left-closed right opening interval, separated by a colon.

2.3len ()

Get string or other type of length

  

test='Alex'v=len (test)print(v)-----4

Where, for example, the length of a list is calculated as a comma

2.4 For Loop to traverse

  

test='Alex' for in test:    print(i)---- -----Alex .

Output strings or other types of values over time.

2.5 Replace

To replace a specific value in a string

  

test='Alex'v=test.replace ('a','  BBBBBBBBBBB')print(v)----------Bbbbbbbbbbblex

In addition, you can limit the number of replacements

  

test='alexa' v1=test.replace ('a','  BBBBBBBBBBB', 1) v2=test.replace ('a','  BBBBBBBBBBB', 2)print(v1,v2)--------------------  Bbbbbbbbbbblexa bbbbbbbbbbblexbbbbbbbbbbb

2.6 Range ()

Range can create consecutive numbers, or you can create discontinuous numbers.

Range (0,100)

>>> Create 0-99 of these sequential numbers, left closed right open interval,

Range (0,100,5)

>>> Create a number that starts at 0 and each 5 is a step

2018-5-27-python Full Stack development day11-string

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.