Python Day 1-getting started with Python (2) simple string operations, python Day 1

Source: Internet
Author: User
Tags string methods

Python Day 1-getting started with Python (2) simple string operations, python Day 1

Data Operations

Some common operations on strings:

1

1 #! /Usr/bin/env python2 2 # coding = utf-83 3 #4 4 # test = 'Hello world' 5 5 # print (test. capitalize () # CAPITAL 6 6 6 # print (test) 7 7 #8 8 #### test. the running result of capitalize does not affect the value of test. So the output result of the following print (test) is helloword.

2

1 # test = 'Hello world' 2 3 # print (test. center (50) # center 4 5 # print (test. center (50, '*') # Add and fill in the neighborhood

3

1 # test = 'Hello world' 2 # print (test. count ('l') # Find the number of characters in the string. If yes, the output is displayed. If no value exists, output 03 # print (test. count ('l',) # Find the number of characters in the string and set the start position and end position

4

1 # test = 'Hello world' 2 # print (test. endswith ('l') # determine the character ending with a string. 3 # print (test. endswith ('D') 4 #5 # print (test. startswith ('D') # determine the character string starting with 6 # print (test. startswith ('H '))

5

1 # test = 'Hello world' 2 # print (test. find ('Z') 3 # print (test. find ('O') # find whether the Sub-character in the string exists. If yes, output the position of the string sequence. -14 # print (test. find ('l',) # Set the start position and end position, and search for characters.

6

1 # test = 'Hello world' 2 3 # print (test. index ('l') # difference between index and find: find is in the search. If it cannot be found,-1 is returned, and index is in the known condition. If it cannot be found, an error is returned.

7

1 # test = 'Hello world' 2 # print (test. isdigit () 3 # test1 = '000000' 4 # test1 = 'aaa1234' 5 # print (test1.isdigit () # Check whether the string is composed of pure numbers

8

# Test = 'Hello world' # test_new = '*'. join (test) # print (test_new) # add other characters between two characters

9

1 # test = 'root: x: 0: 0: root:/bin/bash' 2 # print (test. split (':') 3 # print (test. split (':', maxsplit = 1) # Cut characters by colon. Maxsplit =? '? 'Indicates the maximum number of times to be separated. 4 #5 # test_list = test. split (': ') 6 # print (': '. join (test_list) # interactive call with join

10

1 # test = 'Hello world' 2 3 # print (test. upper () # convert lowercase to uppercase 4 5 # print (test. isupper () # determine whether the string is capitalized

11

1 # test = 'Hello world x' 2 # print (test. strip ('') # strip removes the start and end characters. If it is null in the colon, space 3 # print (test. strip ('x') # If x is not at the beginning or end, no character 4 5 # print (test. rstrip ('x') # Remove the 6 # print (test. lstrip ('') # Remove left and right

12

1 # test = 'Hello world' 2 3 4 # print (test. replace ('','', 1) # replace removes the intermediate characters. For example, if two spaces are to be followed by no parameters, replace them all.

 

Other uncommon string Methods

13

1 # test = 'Hello world' 2 # test1 = 'hello123world' 3 # test2 = 'helloworld' 4 # print (test. isalpha () 5 # print (test1.isalpha () 6 # print (test2.isalpha () # determine whether the string is a pure letter structure. If yes, True is returned. Otherwise, False is returned.

14

1 # test = 'Hello world' 2 # test1 = 'type' 3 # print (test. isidentifier () 4 # print (test1.isidentifier () # determine whether the string is a built-in identifier

15

1 # test = 'Hello world' 2 # test1 = "hello world" 3 # print (test. isupper () 4 # print (test1.isupper () # determine whether the string is capitalized

16

# Test = 'Hello world' # print (test. isprintable () # It is useless to determine whether the data can be printed.

17

1 # test = 'Hello world' 2 # test1 = ''' 3 # print (test. isspace () 4 # print (test1.isspace () # determine whether a string is composed of spaces

18

1 # test = 'Hello world' 2 # test1 = ''3 # print (test. islower () 4 # print (test1.islower () # determine whether a string contains letters. If yes, True is returned and False is returned.

19

1 # test = 'Hello world' 2 3 4 # print (test. istitle () # determine whether the string starts with an uppercase letter. If yes, True is returned, False is returned.

20

1 # test = 'Hello world' 2 # print (test. ljust (20, '*') 3 # print (test. must ust (20, '*') # Left-aligned fill character and right-aligned fill character

21

1 # test = ''' This is first line.2 # This is two line.3 # This is three line.4 # ''' 5 # print (test. splitlines () # split the entire string into line breaks

22

1 test = 'Hello world' 2 3 4 print (test. zfill (20) # Set the string width. leave it blank on the left if it is not enough.

 

 

 

String belongs to the sequence type. A sequence is also called an ordered arrangement, so the characters are also ordered. How can this problem be solved! Let's look at it.

Remember that the python count starts from scratch, so the first character is 0, the second character is 1, the third character is 2, and so on.

String index operations

1

1 # test = 'hello' 2 # print (test [0]) # Cut out the first character 3 # print (test [4]) # Cut out the fourth character 4 # print (test [-1]) # The negative number is from right to left, and-1 is the last one, -2 is the second to the last, etc. 5 6 # test = 'hello' 7 # print (test [1000]) # If the index exceeds the sequence, an error is returned.

 

String splitting

2

1 # test = 'Hello world' 2 # print (test [0: 3]) # Slice starts from 0 to the third character, but ignore the tail in the python head, that is, switch to sequence 2. Remember !!! 3 # print (test) # Any operations on the string will not change the original string unless test = test (test [0: 3]) 4 # print (test [0:]) # From the beginning to the end, the sequence not specified after the colon will be switched to the last 5 # print (test [: 3]) # If no index is specified before the colon, the column starts from 0 and switches to 6 # print (test []) # The step from 0 to 5 is 2, it means that one character is separated into seven characters # print (test [:-1]) # From the back to the front, the whole is reversed

Extract variable

3

1 # test = 'hello' 2 # x, y, z = 'abc' 3 # print (x) 4 # print (y) 5 # print (z) # decompress the variable 6 # x, y, z = 'abc', 'defss', 'xxx' 7 # print (x) 8 # print (y) 9 # print (z)

 

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.