Operator for string loop

1. Operators

1.1 Assignment Operators

  • = += -= *= /= //= %= **=

1.2 Comparison Operators

  • < > = <= == !=

1.3 member operator

  • in not in

1.4 Logical Operators

  • and or not
    • and two are true and taking the foregoing, and the latter two are taken false
    • or both are taken back or the true, false take both of the front or
0 and 9 and 8 and 5 #0,有一假取假
4 or 0 or 9 or 8    #4,有一真取真

Arithmetic operators 1.5

  • + - / // ** %

2. while else

  • while loop the loop condition is satisfied to go, go else condition is not satisfied
  • while walking cycling conditions established body of the loop when it comes to break this cycle is over do not go else

3. Integer

  • python2 in long long integer python3 not, all of type int

4. Boolean value

  • Boolean value is non-zero digit of the time is False True 0
print(bool(0))      #False    
print(bool(-9))     #True
  • Boolean value to a string when the space is not True, space is False
print(bool("你好"))     #True
print(bool(" "))         #False
print(int(True))     #1
print(bool(False))   #0
print(str(True))       #字符串类型的True
print(str(False))      #字符串类型的False

The subscript string

5.1 subscript

  • From left to right number 0 - n
  • To the right operand -1 --- - The length of the string
  • If the index to find out of bounds, on the error
a="你好你好"
print(a[2])       #你
print(a[-3])      #好

5.2 slices, step

  • msg [3: 5] is not taken before and after taking
msg = "今天是个好日子!"
print(msg[3:5])  #个好
  • msg [-5: -2: 1] -25 -2 end position of a starting position of the step 1 is not written default
  • 1 represents -1 for left to right from right to left
msg="今天是个好日子!"
print(msg[-5:-2])  #个好日
msg = "今天是个好日子!下雨了!"
print(msg[2:6:2]) #是好
msg="今天是个好日子!下雨了!"
print(msg[6:1:-1])  #子日好个是
  • If a slice out of bounds, get to the last content

6. The method of string

6.1 s.capitalize()

  • Capitalized
s = "zhyyz"
s1=s.capitalize()
print(s1)        #Zhyyz

6.2 s.upper()

  • ALL CAPS
s = "zhyyz"
s1=s.upper()
print(s1)         #ZHYYZ

6.3 s.lower()

  • All lowercase
s = "ZhYYz"
s1=s.lower()
print(s1)          #zhyyz

6.4 s.count("Y")

  • Returns the number of

6.5 s.endswith("z")

  • To what end

6.6 s.startswith("y")

  • To what end

6.7 s.find("Y")

  • By element lookup to find no subscript -1

6.8 s.index("p")

  • Find index by elements that do not look on error
s = "zhyyz"
print(s.count("y"))      #2
print(s.endswith("z"))   #True
print(s.startswith("y")) #False
print(s.find("y"))        #2

6.9 format() ****

  • String formatting

    s= "zhy"
    s1="zhy{0},{2},{1}"
    s2="zhy{a},{b},{c}"
    print(s.format("你好","yz","少年"))  #zhy
    print(s1.format("你好","yz","少年"))  #zhy你好,少年,yz
    print(s2.format(a="你好",b="yz",c="少年")) #zhy你好,yz,少年

6.10 s.join() ***

  • Insert Character
s= "zhyyzily"
#print(s.join("_"))  #错误
print("_".join(s))   #z_h_y_y_z_i_l_y

6.11 s.split() ****

  • Split
s = "zhyyzilzy"
print(s.print("z"))  #['', 'hyy', 'il', 'y']

6.12 s.strip() ****

  • Spaces on both sides of the end turn Tuotuo newline \ n
s = "       zhyyzily           "
print(s.strip())           #zhyyzily
  • Take off the specified character if there are spaces off spaces
s = "  zhyyzily  "
print(s.strip(" y"))  #zhyyzil

6.13 s.replace ****

  • Replace the first release to be replaced and the second is to replace the contents of
s = "  zhyyzily  "
print(s.replace("z","y"))  #  yhyyyily  

6.14 s.swapcase()

  • Case conversion uppercase converted to lowercase lowercase to uppercase

6.15 s.title()

  • Special sign division of each word capitalized
s = "  zhy yz il y  "
print(s.title())  #  Zhy Yz Il Y  

6.16 s.isdigit ()

  • Judgment is not a pure digital

6.17 s.isalpha()

  • Disc on is not Chinese characters and letters
s = "你好啊zhy撒大声地"
print(s.isalpha())  #True

7. for loop

s="你好嗨啊"               #你
s_len= len(s)            #好
count=0                 #嗨
while count < s_len:     #啊
    print(s[count])
    count += 1
  • for keywords in the keyword variable i object to iterate colon:

    for loop structure

s="你好嗨啊"
for n in s:
    print(n)

Guess you like

Origin www.cnblogs.com/zhaohaiyu/p/11128615.html