Python learning process (7) string

Source: Internet
Author: User
Tags string format

This section describes how to create and manipulate strings.

(1) Create a string:

You can create strings with either single quotation marks or double quotes:

[Email protected]:/home/sunjimeng/Desktop#Cat text.py#both single and double quotation marks can create a stringstr1='Menangel'str2="Sunjimeng"Print(STR1,' is', STR2) [Email protected]:/home/sunjimeng/Desktop#python3.5 text.pyMenangel isSunjimeng[email protected]:/home/sunjimeng/Desktop#

(2) The value specified in the Access string:

Python does not support single character types, and one character is also used as a string in Python.

Python accesses substrings, you can use square brackets to intercept strings:

[Email protected]:/home/sunjimeng/Desktop#Cat text.py#both single and double quotation marks can create a stringstr1='Menangel'str2="Sunjimeng"Print(STR1,' is', str2)Print('str1[0]=', str1[0])Print('Str2[5:9]', Str2[5:9]) [email protected]:/home/sunjimeng/Desktop#python3.5 text.pyMenangel isSunjimengstr1[0]=mstr2[5:9] Meng

(3) Update the value of the string:

[Email protected]:/home/sunjimeng/Desktop#Cat text.py#both single and double quotation marks can create a stringstr1='Menangel'str2="Sunjimeng"Temp='Hello'+str1;Print('the current string is', temp) temp=temp[:6]+str2Print('the current character Fu Cha is', temp) [Email protected]:/home/sunjimeng/Desktop#python3.5 text.pythe current string is Hello Menangel the current word Fu Cha for Hello Sunjimeng

(4) Escape character:

Similar to the C language, Python uses a backslash (\) to escape characters when special characters are needed in characters.

[Email protected]:/home/sunjimeng/Desktop#Cat text.py#1. Continuation testPrint('Menangel is studying Python language!')#2. Test the back slashPrint('path to Windows system files: C:\\document\\menangel')#3. Test output single and double quotation marksPrint('single quotation mark: \ '; double quotation mark: \ "')#4. Test carriage return and line breakPrint('Test line-break \ n')#Leave a linePrint('Test return \ r')#the carriage return has no obvious effect, what is the use? [Email protected]:/home/sunjimeng/Desktop#python3.5 text.pyMenangel isstudying Python language!windows System file path: C:\Document\MenAngel single quote:'; double quotes: "Test line break Test return

(5) string operator:

String operators operate strings in a non-functional way, which is convenient and fast:

Examples of string operators:

[Email protected]:/home/sunjimeng/Desktop#Cat text.pystr1='Menangel'str2='Sunjimeng'#testing of the 1.+ operatorPrint(str1+' '+str2)#testing of the 2.* operatorPrint((str1+' '))#test for 3.[] and [:] OperatorsPrint(str1[0:2],str1[3])#tests for 4.in and not inPrint('M' inchstr1)Print('M'  not inchstr1)#test of 5.R/RPrint('using the escape character specifier'R'\ "I was surrounded."','Become a','\ "I was surrounded."') [email protected]:/home/sunjimeng/Desktop#python3.5 text.pyMenangel Sunjimengmenangel Menangel menangel Me atruefalse after using the escape character \"I was surrounded by the ' become"I was surrounded."

(6) format string%:

Python supports the output of formatted strings. Although this may use a very complex expression, the most basic usage is to insert a value into a string that has the string format of%s.

In Python, string formatting uses the same syntax as the sprintf function in C.

Python string formatting symbols:

format string Output instance:

[Email protected]:/home/sunjimeng/Desktop#Cat text.pyName='Menangel'Sex='male' Age=19Print('My personal information is: name=%s,sex=%c,age=%d'%(name,sex,age)) #方法1Print("My personal information is: name=% (name) s,sex=% (Sex) c,age=% (age) D"%{'name':'Menangel','Sex':'male',' Age': 19}) #方法2 [email protected]:/home/sunjimeng/Desktop#python3.5 text.pyMy personal information is: name=menangel,sex= male, age=19My personal information is: name=menangel,sex= Male, age=19

Formatting operator Auxiliary directives:

(7) Three quotation marks (triple quotes):

Three quotes in Python can copy complex strings: Python three quotes allow a string to span multiple lines, and the string can contain line breaks, tabs, and other special characters.

The syntax for a triple quotation mark is a pair of consecutive single or double quotes (usually paired).

>>> tempstr=" ' hello ... Menangel"print(tempstr) Hello Menangel>>> tempstr'  Hello, \nmenangel'

Three quotation marks Let the programmer out of the mire of quotation marks and special strings, and keep a small piece of string in the form of a so-called WYSIWYG (what is WYSIWYG) format.

A typical use case is that when you need a piece of HTML or SQL, a special string escape is cumbersome when you use a string combination.

" " cursor.execute (  'CREATE TABLE users (  login VARCHAR (8), UID Integer,prid INTEGER)')

(8) Unicode string:

It is simple to define a Unicode string:

>>> temp=u' hello menangel'print(temp) Hello Menangel>>> U'hello\u0020world! The '   #被替换的 \u0020 identity indicates that a Unicode character (a blank space) with encoded value 0x0020 is inserted at the given location.  'Hello world! '

The lowercase "u" before the quotation mark indicates that a Unicode string is created here. If you want to add a special character, you can use Python's Unicode-escape encoding.

Python learning process (7) string

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.