Python full stack __ string first knowledge and operation

Source: Internet
Author: User

Primary data type 1, int int: for calculation.

Decimal converts the number of significant digits into binary.

1 0000 0001

2 0000 0010

3 0000 0011

...    ...

100?

Calculates the number of significant digits converted to binary in decimal: Bit_length ()
i = 100print (I.bit_length ())

2, the string str str: ' Alex ', ' 1235443543 ', ' [A.] '.  A small amount of data can be stored. Index, slice, step index number:

Python12 Period

012345678

Positive index numbered 0,y with forward index number 1,t Positive index numbered 2,h positive index numbered 4,n positive index numbered 5,1 The forward index number for the 3,o is 7, and the positive index number for the period is 8 for the forward indexes that are numbered to the 6,2.

Index:

[Index bit number]

s = ' Python12 period ' S1 = S[0]print (S1,type (S1))

s = ' python12 period ' s2 = s[4]print (s2)

s = ' Python12 period ' s3 = S[2]print (S3)

Python12 Period

-9-8-7-6-5-4-3-2-1

The inverse index number for the period of reverse indexing numbered -1,2 is the reverse index number of -2,1, the reverse index number for -3,n is the inverse index number for the -4,o, and the inverse index number for the -5,h is the reverse index number for the -6,t, and the reverse index number for the -7,y is the reverse number of the -8,p that is numbered-9.

s = ' Python12 period ' S4 = S[-1]print (S4)

Slice:

[Index start bit number: Index end bit number]

Gu Tou In spite of the tail.

s = ' Python12 period ' S5 = S[0:6]print (S5)

The slice starting bit index in brackets is 0 o'clock, which can be omitted.

s = ' Python12 period ' s6 = S[:6]print (S6)

s = ' Python12 period ' s7 = S[1:4]print (s7)

When the index bit ending number in parentheses is omitted, the default slice reaches the end of the string.

s = ' python12 period ' S8 = S[1:]print (S8)

When the index starting bit number in parentheses is omitted from the index end bit number, the entire string is sliced by default, but the result at the slice is not the same as the original string (different storage locations).

s = ' Python12 period ' S9 = S[:]print (S9)

Step:

In brackets, the end bit of the index is followed by the following: The number indicates the size of the intercept, that is, each of the intercepted bits. At the same time can be equidistant intercept.

s = ' Python12 period ' s10 = S[:5:2]print (S10)

s = ' Python12 period ' S11 = S[4::2]print (S11)

When the step is negative, the reverse intercept is indicated.

s = ' Python12 period ' S12 = S[-1:-5:-1]print (S12)

s = ' Python12 period ' S12 = S[-3:-1]print (S12)

Common ways to manipulate strings: * * Capitalize first letter uppercase, the remaining letters lowercase.
s = ' Laonanhai ' S1 = s.capitalize () print (S1)

* Center Center

Center (A, b)-A is the total number of digits of the set string, and is not changed when the total number of digits is less than the number of bits in the string. -B is the string that is populated on the empty number.

s = ' Laonanhai ' s2 = s.center ("*") print (s2)

Upper: All Caps
s = ' Laonanhai ' s3 = S.upper () print (S3)

Lower: All lowercase
s = ' Laonanhai ' s4 = S.lower () print (S4)

Code = ' Qadr '. Upper () Your_code = input ("Please enter a verification code, not case:"). Upper () if Your_code = = code:    Print ("Verification succeeded")

StartsWith decide what content to start with

return bool Value

s = ' Laonanhai ' s5 = S.startswith (' l ') print (S5)

s = ' Laonanhai ' s6 = S.startswith (' Lao ') print (S6)

Returns a bool value that can be sliced and slices separated by commas.

s = ' Laonanhai ' s7 = S.startswith (' N ', 3,6) print (S7)

* Swapcase Case Flip
s = ' Laonanhai ' s8 = S.swapcase () print (S8)

* The first letter of each word not separated by the title is capitalized.
s = ' gdsj Wusir6taibai*ritian ' S9 = S.title () print (S9)

Index Search by Element

Index: Indexed by the element, can be sliced, unable to find an error.

s = ' gdsj Wusir6taibai*ritian ' s10 = S.index (' a ') print (S10)

s = ' gdsj Wusir6taibai*ritian ' s12 = S.index (' Q ') print (S12)

Find: Indexed by element, can be sliced, cannot find return-1.
s = ' gdsj Wusir6taibai*ritian ' S13 = S.find (' a ') print (S13)

s = ' gdsj Wusir6taibai*ritian ' S14 = S.find (' A ', 2) print (S14)

s = ' gdsj Wusir6taibai*ritian ' S15 = S.find (' Q ') print (S15)

\ t: the indent character, which indents the distance of a TAB key.
s = ' \talex ' Print (s)

\ n: Line break.
SS = ' alex\n ' Print (ss)

s = ' \talex\n ' Print (s)

Strip () removes spaces, line breaks, and tabs from the front and back ends.
s = ' \talex\n ' s16 = S.strip () print (S16)

Username = input ("Please Enter account name:"). Strip () if username = = "Wan Rong":    print ("Login Successful")

Strip (' a ') removes the specified character at both ends, and at one end, when the first non-a character is encountered, the end stops the operation and the other end encounters a non-A.
s = '  ablabsexsba ' s17 = S.strip (' a ') print (S17)

Strip (' abc ') when there are more than one character in parentheses, the contents of the parentheses are split into a single, minimal unit, then removed from both ends, and the operation ends when an element that is not in parentheses is encountered.
s = '  ablabsexsba ' s18 = S.strip (' abc ') print (S18)

Lstrip (' a ') removes the a character from the left end.
s = ' Ablabsexsba ' s19 = S.lstrip (' a ') print (S19)

Rstrip (' a ') removes the ' a ' character at the right end.
s = ' Ablabsexsba ' S20 = S.rstrip (' a ') print (S20)

Split () str---> List

The default is separated by a space.

s = ' wusir alex taibai ' S21 = S.split () print (S21)

    

s = ' Wusir,alex,taibai ' S22 = S.split (', ') print (S22)

Split (' A ', b) b is a number, can also be set to be separated by a, then set to separate the first b A, back if there is a, not considered.
s = ' Qwusirqalexqtaibai ' S23 = S.split (' Q ', 2) print (S23)

Join: Join
s = ' alex ' S24 = ' + '. Join (s) print (S24)

In some cases, List---> str.
s = [' Wusir ', ' Alex ', ' taibai ']S25 = '. Join (s) print (S25)

Replace (' A ', ' B ') A is the replaced content and B is the replaced content.
s = ' small matte pink ghlasdfg small pink ' S26 = S.replace (' Little pink ', ' big Hammer ') print (S26)

Replace (' A ', ' B ', C) A is the replaced content, B is the replaced content, and C is a number, meaning to replace the top C a from left to right with B.
s = ' small matte pink ghlasdfg small pink ' s27 = S.replace (' Little pink ', ' big Hammer ', 2) print (S27)

Public method: Len () counts the total number of strings.
s = ' Fdsajlskgjdsaf;jdskfsdaf ' Print (len (s))

Count () calculates the number of occurrences of some elements, which can be sliced.
s = ' Fdsajlskgjdsaf;jdskfsdaf ' S28 = S.count (' F ') print (S28)

Format formatted output three ways: 1, {} placeholder
msg = ' I am {}, this year {}, like {} '. Format (' Taibai ', ' "', ' Girl ') print (msg)

2.
msg = ' I am {0}, this year {1}, like {2}, I still call {0} '. Format (' Taibai ', ' ' "', ' Girl ') print (msg)

3, \ Line break
msg = ' I am {name}, this year {age}, like {hobby}, I still call {name} '    . Format (name = ' White ', age = ' + ', hobby = ' girl ') print (msg)

The Isalnum () string consists of letters or numbers.
name = ' jinxin123 ' Print (Name.isalnum ())

The Isalpha () string consists only of letters.
name = ' jinxin123 ' Print (Name.isalpha ())

The Isalnum () string consists only of numbers.
name = ' jinxin123 ' Print (Name.isdigit ())

For loop: For I in Iteration object: Pass
s = ' fdsagdsfgfdsadsaf ' count = 0while count < Len (s):    print (S[count])    count + = 1

s = ' Fdsagdsfgfdsadsaf ' for I in S:    print (i + ' SB ')

3, BOOL int <-----> str str-----> int:int (str) Condition: The string must all consist of a number.
age = Int (input (">>>")) Print (Age,type (age))

int----->str:str (int)
S1 = str (123) s2 = 123print (S1, type (S1)) Print (S2, type (s2))

BOOL <-----> int True----->1 False----->0
Print (int (True))

Print (int (False))

int----->bool Nonzero is ture and 0 is false.
Print (bool (100))

Print (bool (0))

Print (bool (-1))

BOOL-----> str str (TRUE) str (FALSE) STR-----> BOOL non-null is True, ' empty string is False '.
S1 = "If S1:    print (666)

4. List

[' name ', ture,[] ..... Various types of data, large amounts of data, easy to operate.

5. Tuple tuples

() read-only list, readable and non-writable.

6, Dict

{' name ': ' Old boy ',

' Name_list ': [' negative ',......]

' Alex ': {' age ': 40,

' Hobby ': ' Oldwomen ',

}

},

Store large amounts of data, relational data.

7. Set

{' Wusir ', ' Alex ',...}

Python full stack __ string first knowledge and operation

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.