国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
python每天一道面試題14

Python中str字符串?

字符串是不可變的序列數(shù)據(jù)類型,不能直接修改字符串本身,和數(shù)字類型一樣!Python3全面支持Unicode編碼,所有的字符串都是Unicode字符串。

  • 轉(zhuǎn)義字符
# 字符串前加 u、r、b# u'中文字符組成的字符串'# 作用:以 Unicode 格式 進(jìn)行編碼,一般用在中文字符串前面,防止因為源碼儲存格式問題,導(dǎo)致再次使用時出現(xiàn)亂碼。# r'\n\n\n\n”  # 表示一個普通生字符串 \n\n\n\n,而不表示換行# 作用:去掉反斜杠的轉(zhuǎn)義機(jī)制,常用于正則表達(dá)式,對應(yīng)著re模塊。# b'Hello World’ # 表示這是一個 bytes 對象# 作用:b' '前綴表示:后面字符串是bytes 類型。在網(wǎng)絡(luò)編程中,服務(wù)器和瀏覽器只認(rèn)bytes 類型數(shù)據(jù)。# tip:在Python3 中,bytes 和 str 的互相轉(zhuǎn)換方式是# str.encode('utf-8')和bytes.decode('utf-8')
  • 判斷字符串是不是數(shù)字
def test_string_whether_numeric():    st = 'sd234'    try:        num = float(st)    except (ValueError, TypeError):        print('not numeric')
  • count使用
def test_count(): s = 'The quick brown fox jumps over the lazy dog.' print('the occurrence times of character %s is %s' % ('e', s.count('e')))test_count()# the occurrence times of character e is 3
  • 兩個相同的字符串指向同一內(nèi)存地址
st1 = 'dong'st2 = 'dong'print('st1的內(nèi)存地址==%s\nst2的內(nèi)存地址==%s' % (hex(id(st1)), hex(id(st2))))# st1的內(nèi)存地址==0x21b3f5dc4f0# st2的內(nèi)存地址==0x21b3f5dc4f0
  • 字符串中添加尾隨和前導(dǎo)零
def test_add_trailing_and_leading_zeroes_to_a_string(): st = 'dgfr45sfry4' print('origin string---%s, len(st)---%s' % (st, len(st))) st1 = st.ljust(15, '0') print('add trailing zeroes---', st1) st2 = st.ljust(15, '*') print('add trailing *---', st2) st3 = st.rjust(15, '0') print('add leading zeroes---', st3) st4 = st.rjust(15, '*') print('add leading zeroes---', st4) test_add_trailing_and_leading_zeroes_to_a_string()# origin string---dgfr45sfry4, len(st)---11# add trailing zeroes--- dgfr45sfry40000# add trailing *--- dgfr45sfry4****# add leading zeroes--- 0000dgfr45sfry4# add leading zeroes--- ****dgfr45sfry4
  • zfill使用
def test_combination_3_digit():    nums = []    for x in range(15):        num = str(x).zfill(3)        nums.append(num)    return numsprint(test_combination_3_digit())# ['000', '001', '002', '003', '004', '005', '006', '007', '008', '009', '010', '011', '012', '013', '014']
  • replace方法
st = string1.replace(old, new[, max]) # 會生成一個新對象返回,原來的字符串string1還是原來的值
  • split方法
def get_last_part_string(st):    print(st.split('/'))    print(st.rsplit('/'))    print(st.split('/', 1))    print(st.split('/', 2))    print(st.split('/', 3))    return st.rsplit('/', 1)[0], st.rsplit('-', 1)[0]# split(' ')解決不了單詞間多空格的問題,s.split()可以解決# s = 'a good   example'# s.split(' ')# ['a', 'good', '', '', 'example']# s.split()# ['a', 'good', 'example']# print(get_last_part_string('https://www.w3resource.com/python-exercises/string'))# output:# ['https:', '', 'www.w3resource.com', 'python-exercises', 'string']# ['https:', '', 'www.w3resource.com', 'python-exercises', 'string']# ['https:', '/www.w3resource.com/python-exercises/string']# ['https:', '', 'www.w3resource.com/python-exercises/string']# ['https:', '', 'www.w3resource.com', 'python-exercises/string']# ('https://www.w3resource.com/python-exercises', 'https://www.w3resource.com/python')
  • upper()與lower()
st.upper() # 字符串全大寫st.lower() # 字符串全小寫
  • startswith()
Python startswith() 方法用于檢查字符串是否是以指定子字符串開頭,如果是則返回 True,否則返回 False。如果參數(shù) beg 和 end 指定值,則在指定范圍內(nèi)檢查。語法:startswith()方法語法:str.startswith(str, beg=0,end=len(string));參數(shù):str -- 檢測的字符串。strbeg -- 可選參數(shù)用于設(shè)置字符串檢測的起始位置。strend -- 可選參數(shù)用于設(shè)置字符串檢測的結(jié)束位置。str = 'this is string example....wow!!!';print str.startswith( 'this' );print str.startswith( 'is', 2, 4 );print str.startswith( 'this', 2, 4 );# True# True# False
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Python中用startswith()函數(shù)判斷字符串開頭的教程
python 字符串操作2
Python3 教程 字符串
python中index怎么用
日常校驗要精準(zhǔn):Python字符串處理,字符串頭尾匹配,內(nèi)置函數(shù)匹配與正則匹配
Python str里常用的命令都有哪些?
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服