Python basic data Type (iii)-string concatenation-formatted output-dark copy-python3 notes

Source: Internet
Author: User
Tags iterable natural string shallow copy

1. String concatenation

2. Formatted output

3. God copy and shallow copy

1. String concatenation

Example: a= ' Hello ', b= ' python ', c= '! ' Connect the strings in the a,b,c to a single sentence. 1. A+b+c2 with + number. Format string% '%s%s '% (a,b,c) 3. '. Join () method, note that the brackets are to be connected (can be list, Ganso) '. Join ([a,b,c]) # ' Inside is the character 4 of each string after the connection. The Format method ' {}{}{} '. Format (a,b,c) #{} can be filled with the corresponding symbol in the following format: ' {}{}{} '. Format (A,b,c) when {} is empty, the default index is 0, Enter ' {1}{2}{0} ' in the order of the format brackets. Format (A,B,C) when there is an index value in {}, each subsequent item is filled in ' {n1}{n2}{n3} ' by the previous index value. Format (n1=a,n2=b,n3=c {} You can specify the object name, followed by assigning values to the preceding corresponding value, followed by unordered >>> a,b,c= ' i ', ' love ', ' Python ' #定义字符串 >>> print (a,b,c) I Love python>>> a= ' i '; b= ' love ' #定义字符串 >>> print (A, b) I love plus splicing: >>> a+b+c ' Ilovepyt Hon ' >>> A + ' +b+ ' +c ' I love python ' >>> A + ' +b+ ' * * * * * * +c ' i love**** python ' formatted string % (placeholder) >>> '%s '% 2 ' 2 ' >>> '%s%s%s '% (a,b,c) ' Ilovepython ' >>> '%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 () Method: >>> Help (". Join)" On built-in function Join:join (...) Methods 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 ' >>> ' * '. Jo In ([A,b,c]) ' I*love*python ' >>> ' * '. Join ((a,b,c)) ' I*love*python '. Format mode: >>> Help (". Form AT) 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 is 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, ' I love python ' >>> ' {1} {2} {0} '. Format (a,b,c) #调换索引位置 ' Love Python I ' & Gt;>> ' {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% ' & Gt;>> ' {:. 2%} '. Format (. 222222) #百分比 ' 22.22% ' >>> ' {:. 2%} '. Format (0.222222) #百分比 ' 22.22% ' >>> ' {: <10} '. Format (#输出10位占位, left-aligned '-' >>> ' {: >10} '. Format     #输出10位占位, alignment ' >>> ' {: ^10} '. Format #输出10位占位, centered, 5 each on each side ' 12 ' >>> ' {:* ^10} '. Format #用 * To populate the placeholder ' ****12**** '

2. Formatted output

%s formatted string%d formatted integer%f formatted decimal%c formatted ASCII character%o Format octal%x formatted hexadecimal%e with science and technology format-used as left-aligned + display plus m,n m is the minimum length to display when M is greater than the format number, and n represents the decimal bit transfer character: \ nthe newline \a beep (required in Windows cm  Python in D uses) \b Backspace (required for Python in Windows cmd) \ t Horizontal tab Natural string R ' #格式化字符串 >>> '%s '% 1 ' 1 ' >>> '%s '% ' licky ' licky ' >>> '%10s '% ' lucky ' #10表示字符串的宽度, default right alignment ' lucky ' >>> '%-10 S '% ' lucky ' #-= left aligned ' lucky# formatted integer >>> '%d '% 1 ' 1 ' >>> '%d '% 1.1 ' 1 ' &G T;>> '%d '%-1 '-1 ' #格式化小数 >>> '%f '% 1.22 ' 1.220000 ' >>> '%.3f '% 1.2 #.3 reserved decimal point 3 bits ' 1.200 ' >>> '%2.3f '% 234.1 #指定宽度与实际整数部分宽度冲突以实际输出 ' 234.100 ' >>> '%3.5f '% 1.5 #     Width and progress conflict, followed by precision ' 1.50000 ' >>> '%10.3f '% 1.4 ' 1.400 ' >>> '%-10.3f '% 1.3 ' 1.300 ' #格式化ASCII字符 >>> '%c '% ' a ' >>> '%c '% ' a ' #格式化八进制 >>> '%o '% 8 ' >>> '%o '% 6 ' 6 ' >>> '%o '% + ' #格式化十六进制 >>> '%x '% 16 ' Ten ' >>> '%x '% ' + ' >>> '%x '% ' a ' >>> '%x '% One ' b ' >>> '%     X '% ' F ' #用科学技术法格式化 >>> '%e '% #10的2次方 ' 1.000000e+02 ' >>> '%e '% #10的3次方 ' 1.000000e+03 ' #\n line >>> print (' aaaa\n ') AAAA >>> #\t horizontal tab >>> print (' aaa\tbbb ' ) #\t represents a TAB key. A tab==4 space AAA bbb# Natural String R ' is also called the original string, de-escaping >>> print (R ' aaa\baaa ') aaa\baaa >>> print (' aaa\ \baaa ') aaa\baaa

3. Albums: deep copy and shallow copy (nested between Ganso and list)

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 basic data Type (iii)-string concatenation-formatted output-dark copy-python3 notes

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.