13-day13-str
阿新 • • 發佈:2017-06-07
-c and applet pytho ora -i split() iii 用戶
python第二天string 筆記整理
str2 = [‘a‘, ‘b‘, ‘c‘]
print(tuple(str2))
print(*str2) # string tuple *
# import getpass
# passs = getpass.getpass(‘>>>:‘) #在pycharm裏。這個module是不管用 的
print(1.4e-3) #科學計數法
print(bin(23))
print(oct(23))
print(hex(23)) #進制之間的相互轉化
string方法S
strip 去除指定字符,默認為空格
str1 = ‘*****egon****‘
print(str1)
print(str1.strip(‘*‘))
center 劇中顯示
str2 = ‘hello world‘
print(str2.center(16, ‘#‘))
count
str3 = ‘hel lo world‘
print(str3.count(‘l‘))
print(str3.count(‘l‘, 0, 14)) # 1,14起始終止位置
endswith()
startwith()
find()
format() 字符串格式化的 format方法
str6 = ‘/var/www/html:root:245k‘ print(‘路徑:{},屬於: {},大小: {}‘.format(*str6.split(‘:‘))) print(‘路徑:{0},屬於: {1},大小: {0}‘.format(*str6.split(‘:‘))) str7 = ‘name {},age {} ,salary {}‘ str8 = ‘name {0},age {2} ,salary {1}‘ print(str7.format(‘cx2c‘, 18, 12222)) print(str8.format(‘cx2c‘, 18, 12222))
index()
str9 = ‘hello cx2c‘
print(str9.index(‘c‘))
print(str9[str9.index(‘c‘)])
isdigit()
istitle()
issupper()
ljust()
lower()
replace()
split()
swapcase
索引
str11 = ‘abcdefgh‘
print(str11[0:4]) #前半包
print(str11[0:4:2])
作業
#! /usr/bin/env python # -*- coding: utf-8 -*- # __author__ = "w.z" # Date: 2017/6/7
1. 編寫for循環,利用索引便利出每一個字符
msg = ‘hello egon 666‘
for i in msg:
print(i, end=‘‘)
for ii in range(0, msg.__len__()):
print(msg[ii], end=‘‘)
print(msg[0:msg.__len__()])
2.編寫while循環,利用索引便利出每一個字符
iii = 0
str2 = ‘hello egon 666‘
while iii < str2.__len__():
print(str2[iii], end=‘‘)
iii += 1
3.msg=‘hello alex‘中的alex替換成SB
str3 = ‘hello alex‘
print(str3.replace(‘alex‘, ‘SB‘))
4.msg=‘/etc/a.txt|365|get‘
將該字符的文件名,文件大小,操作方法切割出來
str4 = ‘/etc/a.txt|365|get‘
print(str4.split(‘/‘)[2].split(‘|‘)[0])
print(str4.split(‘/‘)[2].split(‘|‘)[1])
print(str4.split(‘/‘)[2].split(‘|‘)[2])
msg = ‘/etc/a.txt|365|get‘
print("文件名: %s, 文件大小: %s, 操作方法: %s" % tuple(msg.split(‘|‘)))
5.編寫while循環,要求用戶輸入命令,如果命令為空,則繼續輸入
tag = True
while tag:
input_str = input(‘Please input command: ‘)
if input_str.__len__() == 0 or input_str.isspace() is True:
print(‘‘)
else:
tag = False
print(input_str)
continue
6.編寫while循環,讓用戶輸入用戶名和密碼,如果用戶為空或者數字,則重新輸入
tag = True
while tag:
u = input(‘Please input your name: ‘)
if u.__len__() == 0 or u.isdigit() or u.isspace():
u = input(‘Please input your name again: ‘)
else:
p = input(‘Please input your password: ‘)
tag = False
7.編寫while循環,讓用戶輸入內容,判斷輸入的內容以alex開頭的,則將該字符串加上_SB結尾
tag = True
while tag:
str7 = input(‘Please input str: ‘)
if str7.startswith(‘alex‘):
print(str7+‘_SB‘)
8.
1.兩層while循環,外層的while循環,讓用戶輸入用戶名、密碼、工作了幾個月、每月的工資(整數),用戶名或密碼為空,或者工作的月數不為整數,或者
月工資不為整數,則重新輸入
2.認證成功,進入下一層while循環,打印命令提示,有查詢總工資,查詢用戶身份(如果用戶名為alex則打印super user,如果用戶名為yuanhao或者wupeiqi
則打印normal user,其余情況均打印unkown user),退出功能
3.要求用戶輸入退出,則退出所有循環(使用tag的方式)
tag = True
while tag:
username = input(‘username >>>: ‘)
password = input(‘password >>>: ‘)
work_months = input(‘months >>>: ‘)
salary = input(‘salary >>>: ‘)
if username.isspace() is True or username.__len__() == 0 or password.isspace() is True or password.__len__() == 0 or work_months.isdigit() is False or salary.isdigit() is False:
print(‘input wrong‘)
else:
while tag:
print(‘1. 查詢總工資‘)
print(‘2. 查詢用戶身份‘)
print(‘3. 退出登錄‘)
ss = input()
if ss == ‘1‘:
print(int(salary)*int(work_months))
elif ss == ‘2‘:
username2 = input(‘P l i p user: ‘)
if username2 == ‘alex‘:
print(‘super user‘)
elif username2 == ‘wupeiqi‘ or username2 == ‘yuanhao‘:
print(‘normal user‘)
else:
print(‘unknow user‘)
else:
tag = False
continue
13-day13-str