home PYTHONJAVA
 

Python string

The above example execution results:

var1[0]: H
Var2[1:5]: unoo

Python string update

You can intercept a part of a string and splicing it with other fields, as in the following example:

Instance (Python 3.0+)

#!/usr/bin/python3 var1 = 'Hello World!' print ("Updated string: ", var1[:6] + 'welookups!')

The above example execution results

updated strings :  Hello welookups !

Python escape character

Python uses a backslash (\) escape character when you need to use special characters in characters. As shown below:

escape character description
\(at the end of the line) Continuous character
\\ backslash symbol
\' single quotes
\" double quotes
\a Bell
\b Backspace
\e escape
\000 empty
\n Line break
\v Vertical Tabs
\t Horizontal tabs
\r Enter
\f Change page
\oyy octal number, the character represented by yy, for example: \o12 stands for line break
\xyy hexadecimal number, the character represented by yy, for example: \x0a stands for line break
\other Other characters are output in normal format

Python string operator

The following table instance variable a has the value string "Hello" and the b variable value is "Python":

OperatorDescriptionInstance
+String connection a + b Output: HelloPython
*Repeat output string a*2 Output: HelloHello
[]Get the characters in the string by index a[1] Output e
[ : ] Take a section of the string and follow the Left Closed Right principle, str[0,2] does not contain the third character. a[1:4] Output ell
inMember operator - Returns True if the string contains the given character 'H' in a Outputs True
not in member operator - returns True if the string does not contain the given character 'M' not in a output True
r/ROriginal String - Original String: All strings are used literally, without special characters that can be escaped or not printed. The original string has almost exactly the same syntax as a normal string, except that the letter r (which can be capitalized) is preceded by the first quotation mark in the string.
print( r'\n' ) 
print( R'\n' )
%Format string Please see the next section.

Instance(Python 3.0+)

#!/usr/bin/python3 print("a + b Output result:", a + b) print("a * 2 Output result:", a * 2) print("a[1] Output result:", a[1]) print("a[1:4] Output result:", a[1:4]) if( "H" in a) : print("H In the variable a in") a = "Hello" b = "Python" else : print("H Not in variable a ") if( "M" not in a) : print("M Not in variable a in") else : print("M In the variable a in") print (r'\n') print (R'\n')

The output of the above example is:

a + b Output result: HelloPython
a * 2 Output result: HelloHello
a[1] Output result: e
a[1:4] Output result: ell
H In the variable a in
M Not in variable a in
\n
\n

Python string formatting

Python supports the output of formatted strings. Although this may use very complex expressions, the most basic use is to insert a value into a string with the string formatter %s.

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

Instance (Python 3.0+)

#!/usr/bin/python3 print ("My name is %s %d this year!" % ('Jhon', 12))

The output of the above example:

My name is Jhon this year 12 year old!

Format operator helper instructions:

in front of the positive number
symbolfunction
*Define width or decimal precision
-Use left alignment
+Show the plus sign ( + )
<sp>Show spaces before positive numbers
# displays zero ('0') in front of the octal number and '0x' or '0X' in front of the hexadecimal (depending on whether it is 'x' or 'X')
0 The displayed number is padded with '0' instead of the default space
% '%%' outputs a single '%'
(var)Map Variables (Dictionary Parameters)
m.n. m is the minimum total width of the display, and n is the number of digits after the decimal point (if available)

Beginning with Python 2.6, a new function for formatting strings has been added.

*Define width or decimal precision -Use left alignment +Show the plus sign ( + ) in front of the positive number <sp>Show spaces before positive numbers # displays zero ('0') in front of the octal number and '0x' or '0X' in front of the hexadecimal (depending on whether it is 'x' or 'X') 0 The displayed number is padded with '0' instead of the default space % '%%' outputs a single '%' (var)Map Variables (Dictionary Parameters) m.n. m is the minimum total width of the display, and n is the number of digits after the decimal point (if available)

Beginning with Python 2.6, a new function for formatting strings has been added

The above example execution result is:

This is an instance of a multi-line string
Multiple-line strings can use tabs
TAB ( ). 
You can also use line breaks [
  ]. 

Three quotes free the programmer from the quagmire of quotes and special strings, keeping a small string of formatting from start to finish in the so-called WYSIWYG (what you see is what you get) format.

A typical use case is that when you need a piece of HTML or SQL, then string combination, special string escaping will be very cumbersome.

errHTML = ''' <HTML><HEAD><TITLE> Friends CGI Demo</TITLE></HEAD> <BODY><H3>ERROR</H3> <B>%s</B><P> <FORM><INPUT TYPE=button VALUE=Back ONCLICK="window.history.back()"></FORM> </BODY></HTML> ''' cursor.execute(''' CREATE TABLE users ( login VARCHAR(8), uid INTEGER, prid INTEGER) ''')

Unicode string

In Python 2, normal strings are stored in 8-bit ASCII, while Unicode strings are stored as 16-bit unicode strings, which can represent more character sets. The syntax used is to prefix the string with u.

In Python 3, all strings are Unicode strings.


Python's string built-in function

Python's string built-in functions are as follows:






welookups is optimized for learning.© welookups. 2018 - 2019 All Right Reserved and you agree to have read and accepted our term and condition.