int bool str (index, slices) for loop

1. There are variable name = "aleX leNb" do the following:

Removing spaces on both sides of the value corresponding to the variable name, and outputs the processing result,

name = " aleX leNb "
name1=name.strip()
print(name1)

The value corresponding to the variable name in all spaces removed, and outputs the processing result

name = " aleX leNb "
name1=name.replale(' ','')
print(name1)#['aleX', 'leNb']

Determining whether the variable name "al" at the beginning, and outputs the result (+ sliced in two string Method)

name = " aleX leNb "
name1=name.startswith('al')
print(name1)#False

name = " aleX leNb "
name1=name[0:2]
if name1=='al':
  print(True)
else:print(False)

Determining whether the variable name " of Nb ending", and outputs the result (+ sliced in two string Method)

name = " aleX leNb "
name1=name.endswith('Nb')
print(name1)#False

name = " aleX leNb "
name1=name[-2:]
if name1=="Nb":
  print(True)
else:print(False)

The value of the variable name corresponding to all of the "l" is replaced with "p", and outputs the result

name = " aleX leNb "
name1=name.replace('l','p')
print(name1)

The value of the variable name corresponding to the first "l" replaced by "p", and outputs the result

name = " aleX leNb "
name1=name.replace('l','p',1)
print(name1)

The variable name corresponding to the value in accordance with "l" division, and outputs the result

name = " aleX leNb "
name1=name.split('l')
print(name1)
#[' a', 'eX ', 'eNb ']

The variable name corresponding to variable values ​​of all upper case, and outputs the result

name = " aleX leNb "
name1=name.upper()
print(name1)

The variable name corresponding to variable values ​​of all lowercase, and outputs the result

name = " aleX leNb "
name1=name.lower()
print(name1)

Please output of the two character values ​​corresponding to the variable name?

name = " aleX leNb "
name1=name[1]
print(name1)
#a

Please output the first three characters of the name value of the corresponding variable?

name = " aleX leNb "
name1=name[0:3]
print(name1)
# al

Please output the value of two variables corresponding character name?

name = " aleX leNb "
name1=name[-1:-3:-1]
print(name1) # b

2. String s = "123a4b5c"

By the formation of a new string s slices "123"

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

By the formation of a new string s slice "a4b"

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

By forming a string of s5 s slices, s5 = "c"

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

S6 is formed by a string of sections s, s6 = "2ab"

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

S6 is formed by a string of sections s, s6 = "cba"

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

 

3. while loop string s = "hello world" in each element.

s = "你好世界"
count=0
while count<len(s):
    print(s[count])
    count+=1

4. Use of the while loop s = "321" to circulate the contents of the print order is: "Countdown 3 seconds", "2-second countdown", "1 second countdown", "Go!" (Prompt string methods format)

= S '321' 
COUNT = 0 
the while COUNT <len (S): 
    Print ( "% s second countdown '% (S [COUNT])) 
    COUNT = +. 1 
Print (' start ')

5. Use for loop s = "321" to circulate the contents of the print order is: "Countdown 3 seconds", "2-second countdown", "1 second countdown", "Go!" (Prompt string methods format

I in S for: 
    Print ( "% s second countdown '% (I)) 
for I in S: 
    Print (F' second countdown {i} ')

6. Implement a integer addition calculator (two numbers together):

a=input('>>').replace(' ','')#a+b
a1=a.split('+')
s=0
for i in a1:
    s=s+int(i)
print(s)

a=input('>>').strip #13454
s=0
for i in a:
    s=s+int(i)
print(s)

Such as: content = input ( "Please enter Content:") a user input: 9 or 5 + 5 + 5 + 9 or 9 (including a blank), and then divided to calculate the final conversion result obtained integer. (The list also supported the index)

7. Calculate the content entered by the user in a few s character?

a=input('>>').strip()
a1=a.count('s')
print(a1)

Such as: content = input ( "Please enter the text:") as abcassfsqfsafqsdzacsad #

8. Use the while loop are forward and reverse a string message = "Most injuries are cool of the evening sky, gaunt man servant unspeakable." Print.

message = "Most injuries are cool of the evening sky, gaunt man servant unspeakable." 
message1 = the Message [:: - 1] 
Print (message1) 
message = ". Most injuries are cool of the evening sky, gaunt man servant unspeakable" 
COUNT = 0 
the while COUNT <len (Message): 
    Print (Message [COUNT], End = '') 
    COUNT + =. 1 
    
    
Message =. "injury most late cold days, emaciated servant people untold" 
COUNT = 0 
the while COUNT <len (Message): 
    Print (Message [-COUNT-0. 1], End = '') 
    COUNT = +. 1

9. Obtain user input, and calculates the front four "a" appears several times, and outputs the result.

a=input('>>')
a1=a[0:4].count('a')
print(a1)

10. make fun template program needs: wait for user input START Using the name, location, hobbies, names Use according to households and hobbies into ⾏ any reality such as: beloved dear xxx, xxx favorite in places subject, dry xxx (string format of)

INPUT = A ( '>>') 
B = INPUT ( '>>') 
C = INPUT ( '>>') 
Z = F '' 'dear dear {a}, {b} the favorite places subject, dry C} { '' ' 
Print (Z) 

a = INPUT (' >> ') 
B = INPUT (' >> ') 
C = INPUT (' >> ') 
Z =' '' dear dear% s, most ⽅ like, dry ground on% s% s' ''% (A, B, C) 
Print (Z)

11. ⼀ sentence is to determine whether the file back to back files: being the opposite of the idea and the concept is ⼀ kind of example, the Shanghai Auto to tap water and the sea

INPUT = A ( ">>>") 
IF A == A [:: -. 1]: 
    Print ( 'palindromic') 
the else: Print ( 'not')

12. ⼀ START input string, this string is determined requires writing ⺟ zoomed, ⼩ ⺟ write, digital, there has been a total of how many times the other, and outputs it

INPUT = A ( '>>>') 
dxzm = 0 
xxzm 0 = 
SZ = 0 
Qt = 0 
for I in A: 
    IF i.isupper (): 
        dxzm + =. 1 
    elif i.islower (): 
        xxzm + =. 1 
    elif I. isdecimal (): 
        SZ + =. 1 
    the else: Qt + =. 1 
Print (F 'what you type appears zoomed write ⺟ {dxzm} times ⼩ write ⺟ {xxzm} times, digital {sz}, else {qt} views')

13. Sustainable user input (using a while loop), the user used: input A, the road show to go home, and then let the user further choice: choose the bus, or walk? Select the bus, 10 minutes display

Home, and quit the whole program. Choose to walk, displayed 20 minutes home, and quit the whole program. Input B, the show take the path home, and quit the whole program. Enter C, the display detour to go home, and then let the user further choice: choose games hall will play, or the Internet? Select the game room, the show 'one and a half hours to get home, dad at home, waiting for you to take the stick. 'And allowed to re-enter the A, B, C options. Select Internet, then show 'two hours home, my mother has been doing battle. 'And allowed to re-enter the A, B, C options.

True = A 
the while A:
  hjfs the INPUT = ( 'abc')
  IF hjfs.upper () == 'A':
      Print ( 'take the road home, the user further selects the bus, or on foot')
      hjfs1 the INPUT = ( '> >>> ')
      IF hjfs1 ==' bus':
          Print ( 'display 10 minutes home')
          BREAK
      elif hjfs1 == 'walk':
          Print ( 'display 20 minutes home')
          BREAK
  elif hjfs.upper () == 'B':
      Print ( 'road show to go home')
  elif hjfs.upper () == 'C':
      Print ( 'display detour home')
      hjfs2 the INPUT = ( 'play select games hall will, or the Internet?' )
      IF hjfs2 == 'game room':
          Print ( 'one and a half hours to get home, dad at home, waiting for you to take a stick')
          hjfs the iNPUT = ( 'Please re-enter a, B, C options').Strip ()
          Continue
      elif hjfs2 == 'Internet':
          print ( '' two hours home, my mother has been doing battle ready ')
          hjfs the INPUT = (' and allowed to re-enter the A, B, C options')
          the Continue
  Print ( 'the wrong')

Guess you like

Origin www.cnblogs.com/saoqiang/p/11019238.html