您的位置:首页 > 编程语言 > Python开发

python基本数据类型(三)-字符串拼接-格式化输出-深浅复制-python3笔记

2018-03-03 19:00 886 查看
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 基本 数据
相关文章推荐