13-day13-str
生活随笔
收集整理的這篇文章主要介紹了
13-day13-str
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
python第二天string 筆記整理
str2 = ['a', 'b', 'c'] print(tuple(str2)) print(*str2) # string tuple *# import getpass # passs = getpass.getpass('>>>:') #在pycharm里。這個module是不管用 的print(1.4e-3) #科學計數(shù)法
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])作業(yè)
#! /usr/bin/env python # -*- coding: utf-8 -*- # __author__ = "w.z" # Date: 2017/6/71. 編寫for循環(huán),利用索引便利出每一個字符
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循環(huán),利用索引便利出每一個字符
iii = 0 str2 = 'hello egon 666' while iii < str2.__len__():print(str2[iii], end='')iii += 13.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循環(huán),要求用戶輸入命令,如果命令為空,則繼續(xù)輸入
tag = True while tag:input_str = input('Please input command: ')if input_str.__len__() == 0 or input_str.isspace() is True:print('')else:tag = Falseprint(input_str)continue6.編寫while循環(huán),讓用戶輸入用戶名和密碼,如果用戶為空或者數(shù)字,則重新輸入
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 = False7.編寫while循環(huán),讓用戶輸入內(nèi)容,判斷輸入的內(nèi)容以alex開頭的,則將該字符串加上_SB結尾
tag = True while tag:str7 = input('Please input str: ')if str7.startswith('alex'):print(str7+'_SB')8.
1.兩層while循環(huán),外層的while循環(huán),讓用戶輸入用戶名、密碼、工作了幾個月、每月的工資(整數(shù)),用戶名或密碼為空,或者工作的月數(shù)不為整數(shù),或者
月工資不為整數(shù),則重新輸入
2.認證成功,進入下一層while循環(huán),打印命令提示,有查詢總工資,查詢用戶身份(如果用戶名為alex則打印super user,如果用戶名為yuanhao或者wupeiqi
則打印normal user,其余情況均打印unkown user),退出功能
3.要求用戶輸入退出,則退出所有循環(huán)(使用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 = Falsecontinue?
轉載于:https://www.cnblogs.com/cx2c/p/6958765.html
總結
以上是生活随笔為你收集整理的13-day13-str的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: web的几种轮播
- 下一篇: 二叉树的建立及递归遍历