Python Learning day03 int str bool operation function

Source: Internet
Author: User

I. Data type

1. int integer bit_length () returns the binary length of an integral type value

A = The binary code of the   #15 is 1111, of course the storage is in bytes of 00001111, see whether the computer is 32-bit or 64-bit in the front fill 0 print#   the effective length, starting from the first not 0 # 4

2. Str

s="ABCDEFG"s[2]#IndexS[2:3]#slicesS.split (* *)#cut, return to listS.count (* *)#count, return numberS.find (* *)#return IndexS.index (* *)#return IndexLen (s)#returns the length of a string, which is a python built-in function like print

*****the following is a conditional judgment statement that returns TRUE or Falses.startswith (**)#determines whether a string starts with * *, returns TRUE or falseS.endswith (* *)#determines whether the string ends with a * * and returns TRUE or falseS.isalnum ()#determines whether a string consists of letters and numbersS.isdigit ()#determine if a string is a pure Arabic numeralS.isalpha ()#determine if a string is a pure letterS.isnumeric ()#determine if the string is a number, including 123 1000Judgment Statement End ********"abc{}". Format ()#formatted output
String case letter problem ******# string first letter to uppercase S.title ()  # all Words (divided by special characters are considered words, Chinese is also a special character) the first letter becomes uppercase S.upper ()  # becomes capital letter S.lower ()  # becomes lowercase letter S.swapcase ()  # case Swap
S.center (Ten,"*"# is stretched to 10, put the original string in the middle. Rest position Fill *s.expandtabs () S.strip ()   # remove the left and right sides of the space S.strip ("*") # Remove the left and right ends of the   specified element s.replace ( " Original Character "," new character "," number of replacements ")

  

###### #索引s="abcdefg1234567"Print(S[2])#Starting from 0 left to right, so the third character "word" is printed here#CPrint(S[-1])#1 means the last one.#7## # # #切片Print(S[1:4])#The output index position is 1 2 3 out of the character, note that this [] includes only the left number, Gu Tou regardless of the tail#BCDPrint(S[1:4:2])#2 For step, each two output one, output first#BDPrint(S[4:1:-1])#To output from right to left, the step must be negative, that is, when s[First index: Second index: Step], the first index corresponding to the string on the left side of the second index, the step must be positive, the default is 1, the first index corresponding to the string at the second index to the right, the step must be negative#EDCPrint(s[-1:-4:-2])# thePrint(s[1:])#from index 1 to last#bcdefg1234567Print(S[:-1])#from the beginning to the penultimate,-1 is the last, but note [] Gu Tou regardless of the tail#abcdefg123456Print(s[:])#output AS-is
## # # #切割split s="a_b_c_d_e"Li=s.split ("_")#The following lines are used as knives to cut strings, and the left and right sides of all the slides in the original string become two strings and return a list of characters or strings that, as knives, must be cut and cut in at least two copies .Print(LI)#[' A ', ' B ', ' C ', ' d ', ' e ']Li=s.split ("_", 2)#The second parameter is the number of cuts, starting from left to right, and cut two times without cutting.Print(LI)#[' A ', ' B ', ' c_d_e ']Li=s.split ("g")#The string does not have a knife, cannot be tangent, returns the original stringPrint(LI)#[' A_b_c_d_e ']#li=s.split ("") #这个刀为空, error,Li=s.split ("a")#so-called clean break, the left side of a is considered a null characterPrint(LI)#[' ', ' _b_c_d_e ']

 

""" poet scholar exclamation slag slag """ Print (S.split ("\ n"#  cut with \ n

  

Count Number of

"" Print (S.count ("AI")) #   the number of output AI in s string #2

Find Lookup

" tomorrow weekend. What are you going to do? " Print (S.find (" up "))  # If the found content does not exist. Returns-1 if there is a return index # 4 Print (S.find (" i ")) # -1
S.find ("A", 8, 22) # Slicing, finding the index position of "a" from index 8-21

Index Quest Citation

# Index Quest Citation " we are going to have an exam next week. You're not panicking. " Print (S.index (" we want to ")) # 3

Format formatted output

#formatted outputPrint("My name is%s and I'm%d this year."% ("Perfey", 24))#My name Perfey, 24 years old this year.Print("my name is {}, this year {} years". Format ("Perfey", 24))#My name Perfey, 24 years old this year.Print("My name is {1}, this year {0} years old". Format (24,"Perfey"))#You can specify the position of the parameter without having to correspond in order#My name Perfey, 24 years old this year.Print("my name {name}, this year {age}". Format (name="Perfey", age=24))#correspond with meaningful names as parameters#My name Perfey, 24 years old this year.

3. bool

True False

The contents of the string are considered to be true and no content is false. That is, only the "" string is false and the rest of the strings are true

The empty string is false. Non-empty string is true

input employee information into the system, keep typing, enter the return to exit  while True:     = Input (" Enter user information:")    if not name:      When you do not enter a message, the name is empty, and it is        falseto break    Else:        Print (" New Employee information:"+name)

For iteration

 for  Variable in    s  # puts elements in S into a    block of code in a variable

4. List []

5. Tupe () tuple, read-only list, cannot be changed

6. Dict {} dictionary, find faster. Key:value, save two data at a time. A pair of saved

7. Set {}, save one at a time. cannot be repeated. Only key, relatively rare

Python Learning day03 int str bool operation function

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.