乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      python每天一道面試題14

       禁忌石 2023-01-09 發(fā)布于浙江

      Python中str字符串?

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

      • 轉(zhuǎn)義字符
      # 字符串前加 u、r、b# u'中文字符組成的字符串'# 作用:以 Unicode 格式 進(jìn)行編碼,一般用在中文字符串前面,防止因?yàn)樵创a儲存格式問題,導(dǎo)致再次使用時(shí)出現(xiàn)亂碼。# r'\n\n\n\n”  # 表示一個(gè)普通生字符串 \n\n\n\n,而不表示換行# 作用:去掉反斜杠的轉(zhuǎn)義機(jī)制,常用于正則表達(dá)式,對應(yīng)著re模塊。# b'Hello World’ # 表示這是一個(gè) 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
      • 兩個(gè)相同的字符串指向同一內(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]) # 會生成一個(gè)新對象返回,原來的字符串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./python-exercises/string'))# output:# ['https:', '', 'www.', 'python-exercises', 'string']# ['https:', '', 'www.', 'python-exercises', 'string']# ['https:', '/www./python-exercises/string']# ['https:', '', 'www./python-exercises/string']# ['https:', '', 'www.', 'python-exercises/string']# ('https://www./python-exercises', 'https://www./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

        本站是提供個(gè)人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多