Python Basics (data type, string index and slice, string manipulation, for loop)

Source: Internet
Author: User

Data type 1.int: numeric type. Mainly used to calculate

Int:bit_length () converted to a valid number of digits in binary

i = 3i1 = int.bit_length (i) print (I1)

int--->str ' int '--->str (int)

2.STR: String type. Storing a small amount of data for operation with "" means

STR--->int (only digits) str--->int (str)

STR--->bool ""--->false null is False

"0"--->true non-null is True

3.bool: Boolean value. True, False to determine

BOOL--->int True--->1

False--->0

Common:

While True:    passwhile 1: #效率高    pass
4.list: Store large amounts of data with [] to represent 5.dict: dictionaries. Storing data in the form of a key-value pair is represented by {} 6. Meta-ancestor: Read-only, cannot be changed 7. Collection: Intersection, and the index of the set string and slice 1. Index

S = s[serial number]

s = ' ASDFGHJK 's1 = s[0]print (S1) 

"-" represents from the back forward, starting from 1

s = ' ASDFGHJK 's1 = s[-2]print (S1) 

Slices: Gu Tou regardless of tail

S = [serial number: Serial number]

s = ' ASDFGHJK 's1 = s[0:3]print (S1) 

Select All S = [:] or [0:]

s = ' ASDFGHJK 's1 = s[:]print (S1) 

Or

s = ' ASDFGHJK ' S1 = s[0:]print (S1)

S = [First: Tail: Step] The law can only be equal length (step starts at 2)

s = ' ASDFGHJK 's1 = s[0:5:2]print (S1) 
Manipulation of strings

1.s.capitalize () Capitalize first letter

s = ' ASDFG 's1 = str.capitalize (s) print (S1) 

2.s.upper () All Caps

3.s.lower () All lowercase

4.s.swapcase () Case Flip

5.s.title () digits, spaces and special characters are separated by the first letter capitalized

6.s.center () Set length and center, fill to any

7.expandtabs () has \ t Auto-complete 8-bit characters (8 bits for a group)

8.s.startswith () Start with what

9.s.endswith () End With what

10.s.find () index found by element, no return-1

11.s.index () search index by element, cannot find error

12.s.strip () the space before and after the default is removed, which can be specific elements

13.s.split () in what division, eventually form a list this list does not contain this segmented element, str--->list way

14.s.format () formatted output (equivalent to%s)

15.s.replace () Replaces a string, replaces all the elements to be replaced by default, or adds the number of times later

Usage: replace (' old element ', ' new element ', ' number ')

16.s.isalnum () string consisting of letters or numbers

The 17.s.isalpha () string consists only of letters

The 18.s.isdigit () string consists only of numbers

Public methods

Len () query string length

For loop

For....in .... A finite cycle, and the end of the cycle is over.

Python Basics (data type, string index and slice, string manipulation, for loop)

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.