Python-Day-4 Exerice

There are variable name = "aleX leNb" do the following:
1 to remove the spaces on both sides of the value corresponding to the variable name, and outputs the processing results:
name = "aleX leNb"
s1 = name.strip(" ")
print(s1)

  Remove the left variable name "al" and output processing results:

s2 = name.lstrip("al")
print(s2)

  Remove the right variable name "Nb" and output processing results:

s3 = name.rstrip("Nb")
print(s3)

  Remove the variable name beginning with "a" and the last "b", and outputs the result:

s4 = name.strip("a").strip("b")
print(s4)

      Determining whether the variable name beginning with "al", and outputs the result:

s5 = name.startswith("al")
print(s5)

  It determines whether the end of the variable name "Nb", and outputs the result:

s6 = name.endswith("Nb")
print(s6)

  The value of the variable name corresponding to all of the "l" replace "p" and enter the result:

s7 = name.replace("l","p")
print(s7)

  The value of the variable name corresponding to the first "l" replace "p" and enter the result:

s8 = name.replace("l","p",1)
print(s8)

  The variable name corresponding to a value based on all "l" division, and outputs the result:

s9 = name.split("l")
print(s9)

  The variable name corresponding to the first value based on a "l" division, and outputs the result:

s10 = name.split("l",1)
print(s10)

  The name corresponding to the variable value becomes capital, and output:

s11 = name.upper()
print(s11)

  The name corresponding to the variable value becomes lower case, and outputs the result:

s12 = name.lower()
print(s12)

  The variable name corresponding to the first letter of the value of "a" in uppercase, and outputs the result:

s13 = name.capitalize()
print(s13)

  The variable name corresponding to several times the value of the letter "l" appears, and outputs the result:

s14 = name.count("l")
print(s14)

  The previous value of the variable name corresponding to four "l" appears several times, and outputs the result:

s15 = name.count("l",4)
print(s15)

  Found "N" corresponding to the index (if not found, then an error) from the value corresponding to the variable name and output:

s16 = name.index("N")
print(s16)

  Index to find the corresponding "N" from the value corresponding to the variable name (if not found, -1) and outputs the result:

s17 = name.find("N")
print(s17)

  Found "X le" corresponding to the index from the value corresponding to the variable name, and outputs the result:

s18 = name.find("X le")
print(s18)

  Please output of the second character value corresponding to the variable name:

s19 = name[2]
print(s19)

  So please output variable corresponding to the first three characters:

s20 = name[:3]
print(s20)

  Please outputs two character value corresponding to the variable name:

s21 = name[-2:]
print(s21)

  Please output variable name corresponding to the position index "e" where the value:

s22 = name.find("e")
print(s22)
index = 0 # manually record index index 
for C in name:
IF C == "E":
Print (index)
index = index +. 1

 

2- character string s = "123a4b

  1) s by slicing to form a new character bed s1 s1 = "123"

s = "123a4b5c"
print
(s[:3])

  2) by forming a new string s2 s2 slice of s = "a4b"

s = "123a4b5c"
print(s[3:6])

  3) by forming a new string sections s3 s3 of s = "1345"

s = "123a4b5c"
print(s[::2])

  4) by forming a new string s slice s4 s4 = "2ab"

s = "123a4b5c"
print(s[1:6:2])

  5) By forming a new string s5 s5 slice of s = "c"

s = "123a4b5c"
print(s[-1])

  6) new string is formed by slicing s s6 s6 = b2a

s = "123a4b5c"
print(s[-3::-2])

Use loop 3 while and for each print string s = "asdfer" each element:

  

The for loop 4 using s = "asdfer" circulate, but the content of each print is "asdfer"

s = "asdfer"
for c in s:
    print(s)

Use for loop 5 s = "abcdefg" circulation content per print each character plus sb.

s = "dfdffdferetyhjhb"
for c in s:
    print(c+"sb")

The for loop 6 using s = "321" to circulate the contents of the print order is:! "Starting" "countdown 3 seconds", "2-second countdown", "1 second countdown"

= S " 321 " 
for c in S:
     Print ( " Countdown seconds% s " % c)
 the else :
     Print ( " start! " )

7, to achieve a calculator integer addition (two numbers):
  as: countent = input ( "Please enter Content:!") A user input: 9 or 5 + 5 + 5 + 9 or 9, and then further divided calculation:

the INPUT = shizi ( " Please input the content you want to calculate: " ) 
shizi = shizi.replace ( "  " , "" ) # get rid of all spaces 
LST = shizi.split ( ' + ' ) # start cutting from the + sign, the result is the cutting List 
Print (int (LST [0]) + int (LST [. 1])) # string transfected int (integer)

9, the user input content is calculated Several integer (in bit units digit)

  Such as: count = input ( "Please enter the text:") as fhdal1234slfh98769fjdla #

= zhenshu 0 
Content = INPUT ( " Please enter Content: " )
 for C in centent:
     IF c.isdigit (): 
        zhenshu = zhenshu +. 1
 Print (zhenshu)

10, write code, complete the following requirements:

And continuous user input (while loop), the use of the user:

Input A, take the road home is displayed, and then let users further options:

  Choose the bus, or walk?

  Select the bus, show 10 minutes the home, and quit the whole program.

  Choose to walk, displayed 20 minutes home, and launched the entire program.

Input B, the show take the path home, and launched the entire program.

C. input is displayed detour to go home, and then let the user further choice:
  choose games hall will play, or Internet cafes.

  Select the game rooms, it shows "a half hours to get home, dad at home, waiting for you to take the stick." And allow you to re-enter A, B, C, option.

11. The write code: calculating the sum of 1 + 2 + 3 + 99 ...... 88 in addition to the number of all?

the while 1 : 
    Content = the INPUT ( " Please enter the course you have to choose: A, B, C: " )
     IF Content == " A " 
        Print ( " take the road home " ) 
        traffic = the INPUT ( " Excuse me, choose buses or simply walking. " )
         IF traffic == " bus " 
            Print ( ' 10 Fenzhong home ' )
         elif traffic == " walking " 
            Print ( " 20 Fenzhong home . ")
        the else :
             Print ( ' still waiting ' )
         BREAK 
    elif Content == " B " 
        Print ( ' take the path home ' )
         BREAK 
    elif Content == " C " 
        Print ( " detour home " ) 
        Play = the INPUT ( " Please choose to go to Internet cafes, or game room? " )
         IF Play == " Internet cafes " 
            Print ( " two hours to go home, my mom was ready for the battle. ")
        elif Play == " game room " 
            Print ( " a half-hour to go home, dad at home, waiting for you to take the stick. " )
         the else :
             Print ( " bath !!! " )
     the else :
         Print ( " roll-calf do not make trouble ." )
count = 1
sum = 0
while count <= 99:
    if count == 88:
        pass
    if count %2 == 0:
        sum = sum - count
    else:
        sum = sum + count
    count = count + 1
print(sum)

 

Guess you like

Origin www.cnblogs.com/marksi/p/11487537.html