Learning & python for loop operation of the work string

A, for circulation

Nested loop of 1.1 for 99 print multiplication table

for i in range(1,10):
    for j in range(1,i+1):
        print('{}*{}={:<2}'.format(j,i,i*j),end=' ')
    print()

Nesting of 1.2 for loop print pyramid

method one:

num = input('请输入金字塔的层数:')
num = int(num)
for i in range(1,num+1):
    for j in range(num-i):
        print(' ',end = '')
    for k in range(2*i-1):
        print('*',end = '')
    print()

Method Two:

num = input('请输入金字塔的层数:')
num = int(num)
for i in range(1,num+1):
    print(' '*(num-i)+'*'*(2*i-1))

1.3 for + range with rewriting the morning the dictation code

user_name = 'vincent'
password = '666'
for i in range(3):
    inp_name = input('请输入你的账号:').strip()
    inp_pwd = input('请输入你的密码:').strip()
    if inp_name == user_name and inp_pwd == password:
        print('登录成功。')
        while True:
            cmd = input('请输入执行命令>>>').strip()
            if cmd == 'q':
                break
            else:
                print('{x}命令正在执行。'.format(x=cmd))
        break
    else:
        print('账号或密码错误。')
else:
    print('3次账号或密码输入错误,退出程序。')

Second, string manipulation

Write the code, the following variables, functions as required to implement each (a total of 6 points, 0.5 points each Meixiao Ti)

= name "Alex"
. 1) removed on both sides of the value corresponding to the variable name spaces, and outputs the processing result

name = " aleX"
res = name.strip()
print(res)              #aleX

2) determination whether the value corresponding to the variable name beginning with "al", and outputs the result


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

3) corresponding to the variable name is determined whether the value of an "X" at the end, and outputs the result


name = " aleX"
res = name.endswith('X')
print(res)              #True

4) the value corresponding to the variable name in the "l" is replaced with "p", and outputs the result

name = ' aleX'
res = name.replace('l','p')
print(res)

5) The name of the corresponding variable value in accordance with "l" division, and outputs the result.

name = ' aleX'
res = name.split('l')
print(res)

6) The name becomes a variable value corresponding to upper case, and outputs the result


name = ' aleX'
res = name.upper()
print(res)

7) The name corresponding to the variable value becomes lower case, and the output


name = ' aleX'
res = name.lower()
print(res)

8) Please outputs of the two character values ​​corresponding to the variable name?

name = ' aleX'
print(name[1])

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

name = ' aleX'
print(name[0:3])

10) Please post the output value of two variables corresponding character name?


name = ' aleX'
print(name[-1:-3:-1])

11) Request output value corresponding to the variable name index where "e" position?


name = ' aleX'
print(name.index('e'))

12) acquisition sequences, removing the last character. Such as: oldboy then get oldbo.

name = ' aleX'
res = name[0:-1]
print(res)

Guess you like

Origin www.cnblogs.com/leilijian/p/12456877.html