Python-lesson Three

Source: Internet
Author: User
Tags string format

Python

Schoolboys

List API

Sequence type

List generation

List Api:

1.pop () Popup element by index, return value for that element, default popup last Element

Pop (positive Value) from index position

Script

g=[0,1,2,3,4]

Print (g.pop ())

Execution results

4

Pop (negative Value) make subscript from the bottom of the list

Script

g=[0,1,2,3,4]

Print (g.pop (-2))

Execution results

3

Remove ():

Remove removes a value from the list instead of deleting the subscript value

(remove (the Middle is not the subscript value, is the Value)) delete only the first occurrence return value

Script

A= ' 12345678934343 '

G=list (a)

Print g

G.remove (' 3 ')

Print g

Show results

[' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' 3 ', ' 4 ', ' 3 ', ' 4 ', ' 3 ']

[' 1 ', ' 2 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' 3 ', ' 4 ', ' 3 ', ' 4 ', ' 3 ']

Count (count The number of occurrences of the set Value)

g=[1,2,3,4,3,4,3]

G.conut (' 3 ')

Show results

3

3 This value appears three Times.

Reverse (reverse Storage)

Script

g[1,2,3]

G.reverse ()

Print g

Show results

[3,2,1]

Various loop statements are used

Script

A= ' 123 '

G=list (a)

For I in G:

Print I

Show results

1

2

3

The script can display subscript!

A= ' 123 '

G=list (a)

For I,val in enumerate (g):

Print (i,val)

Show results

(0, ' 1 ')

(1, ' 2 ')

(2, ' 3 ')

Stop (default Ascending sort)

Variable. Stop () This is sort done

Can be sorted by 123456789 or abcdefg-----

Sequence type

string, list, Meta-ancestor

String link Repetition

Print (variable 1+ variable 2) two variables displayed together

Print (variable 1*3) variable 1 shows three times

Whether an element is included in the interpretation character

Print (a value in the variable in variable 1) see if the value in variable 1 has a return ture not return false

Len (statistics list Length)

Isalpha () See if It's all letters

IsDigit () to see if the skirt type number

Upper () turn all the English into uppercase

Lower () all letters are lowercase

Slice

Can have two colons, divided into three segments

Print (variable [: 3]) the No. 0 position starts to a third

Print (variable [0:]) from 0 position to last

Print (variable [:: 2]) from start to end, output objects every two times

Formatted output: use Python to output various types of strings

Syntax: Print "string"%formatl%fornat2.

string:%s

shaping:%d

Floating point type:%f

Script

Name= ' Gaoxingzhou '

Print (' His name '%s '% (name)) Inserts the name string format into%s

Show results

His name is Gaoxingzhou


Name= ' Gaoxingzhou '

name1= ' Chinese '

Print (' I am%s ' is%s '% (name1,name)) % after the variable installation order is assigned to%s

Show results

I am Chinese his name is Gaoxingzhou


s=1.123456789

Print (' num%.0f '% (s)) . 0 is reserved 0 decimal places. 2 is two,

Cast type

int (forced to Reshape) example int (variable 1)

Float (float Type)

STR (string)

Common methods in strings

Name= ' Gaoxingzhou '

Print (name.find (' g ')) looks for g in the name variable if there is a return of 0 no return-1

The characters present in the print (name.split (' o ')) variable are used as separators to split the variable

A= ' 0123456 '

Print (' + '. Join (a)) adds a + sign to the middle of a character in a as a delimiter

Print (a.replace (' 0 ', ' 22222 ')) replaces the character 0 in the A variable with 22222

Print (a.strip (0123)) Delete Open and several characters, or delete a string, example is delete 0123

This article is from the "11627223" blog, please be sure to keep this source http://11637223.blog.51cto.com/11627223/1914982

Python-lesson Three

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.