Python string: Part 1

Source: Internet
Author: User

Python string: Part 1

----String: A collection of ordered characters

1, single and double quote strings are the same, single quotes embedded in double quotation marks or double quotation marks embedded in single quotation marks, single quotation marks embedded in single quotation marks or double quotation marks in double quotation marks error, need to escape the character. #报错信息: Syntaxerror:invalid Syntax (syntax error: Invalid syntax)

2. Commonly used escape characters: \ n line break; \ t Tab key (2 bytes); \a Bell; <--this is 0 Null

3, the escape character after the content is meaningless, the escape character remains in the original notation in the variable.

4, three quotes represent a block string. < But many times it is used to comment out multiple lines of code >

5, the string can be combined by +, through the * to repeat, through Len () to find the length.

In []: Len ('ABC') out[44]: 3In [45]:'ABC'+'CDE'out[45]:'ABCCDE'In [46]:'Hello'*6out[46]:'Hellohellohellohellohellohello'In [47]:'-'*80out[47]:'--------------------------------------------------------------------------------'In [48]:Print('-'*80)--------------------------------------------------------------------------------In [Further]: myjob='Doctor'In [50]: forIinchmyjob: ...:Print(i,end=' ') d o c t o rIn [53]:'o' inchmyjobout[53]: Truein [55]:'ob' inchmyjobout[55]: Falsein [56]:'ob' inch "Obb"out[+]: True

6, the index value of Python starts at 0, the maximum value is the string length minus 1. The rightmost value of the Shard does not contain the right boundary. The third limit for a shard is stepping, and a negative number is a right-to-left, while the left-to border reverses.

In [s=]:'abcdefghijklmnopqrstuvwxyz'In [75]: sout[75]:'abcdefghijklmnopqrstuvwxyz'In [[]: S[1:10]out[76]:'Bcdefghij'In [[S[1]::] out[77]:'bcdefghijklmnopqrstuvwxyz'In [[S[:10]:]out[78]:'Abcdefghij'In [[S[:-1]:]out[79]:'Abcdefghijklmnopqrstuvwxy'In [[]: S[1:25:2]out[80]:'BDFHJLNPRTVX'In [Bayi]: S[::2]out[81]:'Acegikmoqsuwy'In [[S[::-1]:]out[82]:'ZYXWVUTSRQPONMLKJIHGFEDCBA'In [[S[8:4:-2]:]out[83]:'IG'

7. String Conversion Tool: Int () to Integer, str () to String, repr () to String, float () to floating point, Ord () to corresponding ASCII value; Chr () is the reverse operation of Ord.

# Binary conversion to 10 binary In [94]: b='1101'in[the]: i=0In [ while b!=':    ...:     I=i*2+ (Ord (b[0])-ord ('0')) ...    :     B=b[1:]    ...: in [98]: iout[98]: 13

8, modify the string can not be modified in situ, you can use the combined method or the. Replace method. Example of formatted output:

In [102]: s='ABC'In [103]: sout[103]:'ABC'In [104]: s[0]out[104]:'a'In [[s[0]=]:b---------------------------------------------------------------------------TypeError Traceback (most recent)<ipython-input-105-47eb3968d8a6>inch<module>()----> 1 s[0]=Btypeerror:'Str'Object does notSupport Item Assignment
 in [106]: S=s[:4]+ " bbb  "  in [ 107]: sout[ 107]:  " abcbbb   " in [ 108]: s.replace ( " bbb  " ,  " CCC  "  ) out[ 108]:  " ABCCCC  "  
in [+]: name='dereck'in[111]: age=30 in[]: city='   Tianjin'in[print('Hello everyone,i am%s,i am%d years old, I comes from %s' % from Tianjin

====== the End ======

Python string: Part 1

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.