python基礎資料型別 (Elementary Data Type)(三)-字串拼接-格式化輸出-深淺複製-python3筆記

來源:互聯網
上載者:User

標籤:python基礎資料型別 (Elementary Data Type)   字串拼接-格式化輸出-深淺複製   python3   

1.字串拼接

2.格式化輸出

3.神複製和淺複製

1.字串拼接

例: a=‘hello‘, b=‘python‘,c=‘!‘ 將a,b,c中的字串連成一句話。1.用+號a+b+c2.格式化字串 %‘%s %s %s‘ % (a,b,c)3.‘‘.join()方法,注意括弧是要串連的(可以是列表,元祖)‘ ‘.join([a,b,c])  #‘‘裡面是串連後各個字串的字元4. .format方式‘{}{}{}‘.format(a,b,c)   #{}裡面可以填入與後面相對應的符號format方法詳解:‘{}{}{}‘.format(a,b,c)當{}裡面為空白的時候,裡面預設索引為0,1,2按format括弧裡面的順序依次填入‘{1}{2}{0}‘.format(a,b,c)當{}裡面有索引值時,按前面的索引值將後面的每項依次填入‘{n1}{n2}{n3}‘.format(n1=a,n2=b,n3=c){}裡面可以指定對象名稱,後面通過賦值給前面的相應的值,後面是無序的>>> a,b,c=‘I‘,‘love‘,‘python‘   #定義字串>>> print(a,b,c)I love python>>> a=‘I‘;b=‘love‘              #定義字串>>> print(a,b)I love加號拼接:    >>> a+b+c    ‘Ilovepython‘    >>> a+ ‘ ‘ +b+‘ ‘+c    ‘I love python‘     >>> a+ ‘ ‘ +b+‘**** ‘+c    ‘I love**** python‘格式化字串 % (預留位置)    >>> ‘%s‘ % 2    ‘2‘    >>> ‘%s%s%s‘ % (a,b,c)    ‘Ilovepython‘    >>> ‘%s %s %s‘ % (a,b,c)    ‘I love python‘    >>> ‘%s %s ** %s‘ % (a,b,c)    ‘I love ** python‘    >>> ‘%s + %s =%s‘ % (1,2,3)    ‘1 + 2 =3‘‘‘.join()方法:    >>> help(‘‘.join)    Help on built-in function join:    join(...) method of builtins.str instance        S.join(iterable) -> str        Return a string which is the concatenation of the strings in the        iterable.  The separator between elements is S.     >>> ‘‘.join([a,b,c])    ‘Ilovepython‘    >>> ‘ ‘.join([a,b,c])    ‘I love python‘    >>> ‘*‘.join([a,b,c])    ‘I*love*python‘    >>> ‘*‘.join((a,b,c))    ‘I*love*python‘.format方式:    >>> help(‘‘.format)    Help on built-in function format:    format(...) method of builtins.str instance        S.format(*args, **kwargs) -> str        Return a formatted version of S, using substitutions from args and kwargs.        The substitutions are identified by braces (‘{‘ and ‘}‘).    >>> print(a,b,c)    I love python    >>> ‘{}{}{}‘.format(a,b,c)    ‘Ilovepython‘    >>> ‘{} {} {}‘.format(a,b,c)    ‘I love python‘    >>> ‘{0} {1} {2}‘.format(a,b,c)  #預設索引位置為0,1,2    ‘I love python‘    >>> ‘{1} {2} {0}‘.format(a,b,c)  #調換索引位置    ‘love python I‘    >>> ‘{0[0]} {0[1]} {0[2]}‘.format([a,b,c])    ‘I love python‘    >>> ‘{n1} {n2} {n3}‘.format(n1=a,n2=b,n3=c)    ‘I love python‘    >>> ‘{n1} {n3} {n2}‘.format(n1=a,n2=b,n3=c)    ‘I python love‘#format補充    >>> ‘{:.1f}‘.format(12.2222)        #保留1位小數    ‘12.2‘    >>> ‘{:.2%}‘.format(12.22222)       #百分比    ‘1222.22%‘    >>> ‘{:.2%}‘.format(.222222)        #百分比    ‘22.22%‘        >>> ‘{:.2%}‘.format(0.222222)       #百分比    ‘22.22%‘    >>> ‘{:<10}‘.format(12)             #輸出10位佔位,靠左對齊    ‘12        ‘    >>> ‘{:>10}‘.format(12)             #輸出10位佔位,有對齊    ‘        12‘    >>> ‘{:^10}‘.format(12)             #輸出10位佔位,置中,兩邊各5各    ‘    12    ‘    >>> ‘{:*^10}‘.format(12)            #用*來填充佔位    ‘****12****‘

2.格式化輸出

%s  格式化字串%d  格式化整數%f  格式化小數%c  格式化ASCII字元%o  格式化八進位%x  格式化十六進位%e  用科學技術法格式化- 用作靠左對齊+ 用在整數前面顯示加號m,n m是顯示的最小長度,當m大于格式化為數時才起作用顯示m位,n代表小數的位元轉移字元:\n  換行  \a提示音(需在windows的cmd中的python使用)  \b退格鍵(需在windows的cmd中的python使用)      \t橫向定位字元自然字串  r‘‘#格式化字串    >>> ‘%s‘ % 1    ‘1‘    >>> ‘%s‘ % ‘licky‘    ‘licky‘    >>> ‘%10s‘ % ‘lucky‘        #10表示字串的寬度,預設靠右對齊    ‘     lucky‘    >>> ‘%-10s‘ % ‘lucky‘       #-表示靠左對齊    ‘lucky#格式化整數    >>> ‘%d‘ % 1            ‘1‘    >>> ‘%d‘ % 1.1    ‘1‘    >>> ‘%d‘ % -1    ‘-1‘#格式化小數    >>> ‘%f‘ % 1.22    ‘1.220000‘    >>> ‘%.3f‘ % 1.2        #.3保留小數點後3位    ‘1.200‘    >>> ‘%2.3f‘ % 234.1     #指定寬度與實際整數部分寬度衝突以實際輸出    ‘234.100‘    >>> ‘%3.5f‘ % 1.5       #寬度和進度衝突,遵循後面的精度    ‘1.50000‘    >>> ‘%10.3f‘ % 1.4    ‘     1.400‘    >>> ‘%-10.3f‘ % 1.3    ‘1.300     ‘#格式化ASCII字元    >>> ‘%c‘ % 65    ‘A‘    >>> ‘%c‘ % 97    ‘a‘#格式化八進位    >>> ‘%o‘ % 8    ‘10‘    >>> ‘%o‘ % 6    ‘6‘    >>> ‘%o‘ % 32    ‘40‘#格式化十六進位    >>> ‘%x‘ % 16    ‘10‘    >>> ‘%x‘ % 32    ‘20‘    >>> ‘%x‘ % 10    ‘a‘    >>> ‘%x‘ % 11    ‘b‘    >>> ‘%x‘ % 15    ‘f‘#用科學技術法格式化    >>> ‘%e‘ % 100      #10的2次方    ‘1.000000e+02‘    >>> ‘%e‘ % 1000     #10的3次方    ‘1.000000e+03‘#\n 換行    >>> print(‘aaaa\n‘)    aaaa    >>> #\t橫向定位字元    >>> print(‘aaa\tbbb‘)       #\t表示一個tab鍵。一個tab==4個空格    aaa bbb#自然字串  r‘‘  也叫原始字串,取消轉義    >>> print(r‘aaa\baaa‘)    aaa\baaa    >>> print(‘aaa\\baaa‘)    aaa\baaa

3.專輯:深複製和淺複製(元祖和列表之間的相互嵌套)

1.元祖和列表之間的相互嵌套(字串裡面都會變成字串,失去列表和元祖的方法)2.嵌套之後可以通過索引值來去數3.淺複製4.深複製5.那些是淺複製 copy 切片#淺複製    >>> li=[1]    >>> id(li)    47093800    >>> li1=li.copy()    >>> id(li1)    47094840    >>> li = [‘a‘,‘b‘]    >>> li_1 = [1,li]    >>> li_1    [1, [‘a‘, ‘b‘]]    >>> lq = li_1.copy()    >>> lq    [1, [‘a‘, ‘b‘]]    >>> li.append(‘c‘)    >>> lq    [1, [‘a‘, ‘b‘, ‘c‘]]    >>> id(li)    46524336    >>> id(lq[1])    46524336#深複製    >>> import copy    >>> ls = copy.deepcopy(li_1)    >>> ls    [1, [‘a‘, ‘b‘, ‘c‘]]    >>> li    [‘a‘, ‘b‘, ‘c‘]    >>> li.append(‘d‘)    >>> ls    [1, [‘a‘, ‘b‘, ‘c‘]]    >>> id(li)    46524336    >>> id(ls[1])    47011280

python基礎資料型別 (Elementary Data Type)(三)-字串拼接-格式化輸出-深淺複製-python3筆記

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.