1. 程式人生 > >python基本數據類型(三)-字符串拼接-格式化輸出-深淺復制-python3筆記

python基本數據類型(三)-字符串拼接-格式化輸出-深淺復制-python3筆記

python基本數據類型 字符串拼接-格式化輸出-深淺復制 python3

1.字符串拼接

2.格式化輸出

3.神復制和淺復制

1.字符串拼接

例: a=‘hello‘, b=‘python‘,c=‘!‘ 將a,b,c中的字符串連成一句話。
1.用+號
a+b+c
2.格式化字符串 %
‘%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基本數據類型(三)-字符串拼接-格式化輸出-深淺復制-python3筆記