Both single and double quotation marks can represent strings. The difference is when escaping.
If you are lazy to add an escape character, you can precede the string by adding R. For example:
Print R ' C:\some\name '
Do not wrap by adding a backslash to the string.
Print "" \ usage:thingy [OPTIONS]- h Display This Usage message- h hostname hostname to connect to< c5/> "" "
strings are connected by a plus sign and can be doubled by multiplication sign.
Strings can also be concatenated by writing them together, but not on variables:
' Py ' ' thon '
The string can be accessed like an array, and 0 represents the start character. In particular, 1 represents the last character, 2 is the 2nd character, and the result is obtained sequentially.
strings can be accessed by slicing. In particular, the use of negative numbers to slice.
s= "ABCDE" s[0]s[-1]s[-5]s[:-1] #去掉最后一个字符, such as line break +---+---+---+---+---+---+| P | y | T | H | o | N |+---+---+---+---+---+---+0 1 2 3 4 5 6-6-5-4-3-2-1
Slicing access out of bounds will get an empty set. No access control is required.
Cannot assign a value to a single character because the string is immutable. If you need a different string, then creat a new string, which can be easily achieved by using slices.
The built-in function Len returns the length of the string.
Use encode and decode to ask for string encoding and decoding. (For questions about the type of encoding, you need to open a file specifically to discuss)
Common methods:
StartsWith Detection Start
Example:
1. A = ' Leonis '
If A.startswith (' Le '):
print ' Le '
EndsWith Detection End
Example:
1. A = ' Leonis ' If A.endswith (' is '):p rint ' is '
In detects if it is part of
Example:
1. A = ' Leonis ' if ' o ' in A:print ' a '
The Find query contains
Example:
1. a= ' Leonis ' if A.find (' on ')! = -1:print (' on ')
Join connection string
Example:
1.
A = ' '
MyList = [' Brazil ', ' Russia ', ' India ', ' China ']print a.join (mylist)
2.
MyList = [' Brazil ', ' Russia ', ' India ', ' China ']print ' _ '. Join (MyList)
Split splitting string
1.
b = ' my '. Name: Is.. Leonis ' Print bprint b.split ('.. ')
2.
b = ' my '. Name: Is.. Leonis ' Print bprint b.split ('.. ', 1)