Get more information Welcome to my website or csdn or [blog Park]
After the introduction, I believe you have a preliminary understanding of Python, this section mainly introduces the string, regardless of learning what language string must play an important role in it. This section focuses on the definition of strings and how strings are used;
Python Getting Started list and tuples
Software installation for getting started with Python
Introduction to Strings
A string is a sequence that is used to represent and store text. Once a string is declared in Python, it cannot be changed. So also known as String constants;
Python actually has three types of strings:
1. Single double quote string:
>>> ‘peace‘‘peace‘>>> "peace"‘peace‘>>>
2. Three quote string and escape string, raw raw string:
A three-quote string is allowed to wrap, and the raw raw string is not escaped;
#三引号字符串‘‘‘peace... peace... peace‘‘‘‘peace\npeace\npeace‘#转义字符串‘Im‘peace‘ File "<stdin>", line 1 ‘Im‘peace‘ ‘Im\‘peace‘"Im‘peace"#原始字符串 不进行转义:print (r"c:\peace\n"print ("c:\peace\n")c:\peace
3Unicode string:
>>> print(u‘peace\u0020one‘)peace one>>>
Basic string manipulation
All standard sequence operations (indexes, shards, multiplication, judging membership, length, maximum and minimum values) are also appropriate for strings, but the strings are immutable, index assignments and shard assignments are not allowed;
>>> name="peace">>> name[1]=‘q‘Traceback (most recent call last): "<stdin>"1in‘str‘not support item assignment>>>
String Formatting 1.% character: start of tag translator
string format conversion type:
The demo is as follows:
#%s 格式化字符串:format="I\‘m,%s,%s?">>> name=(‘One‘,‘peace‘print (format % name)I‘m,One,peace?#%f格式化实数:>>> format="1.31400 with three decimals:%.3f">>> print(format % 1.31400)1.31400 with three decimals:1.314>>>
2. Conversion flag:
-: Align Left
+: Add a positive sign before converting a value
"": Leave blank before positive number
0: Conversion value if the number of bits is not enough with 0 padding
. Or. *: Output accuracy
Here I have a single-precision symbol number to demonstrate:
The above actions are also available for other types
1. Left alignment in the% number plus-number;
>>> a=1.314520>>> ‘%010.3f‘%a‘000001.315‘>>> ‘%-010.3f‘%a‘1.315 ‘>>>
2. Add plus sign before output:% followed by +
>>> a=1.314520>>> ‘%+f‘%a‘+1.314520‘>>>
3. Leave blank before positive number:% plus above
>>> a=1.314520>>> b=-a>>> b-1.31452>>> ‘%f‘%b‘-1.314520‘>>> ‘% f‘%a‘ 1.314520‘
4. The output bit number is not enough 0 supplement: the% number is followed by 0
>>> a=1.314520>>> ‘%010f‘%a‘001.314520‘>>>
5. Output accuracy: The% number is added after. or. * If used, the precision value is read from the tuple parameter
>>> a=1.314520>>> ‘%.3f‘%a‘1.315‘>>> ‘%.*f‘%(3,a)‘1.315‘>>>
String method: Find Method
The Find method can find substrings in a longer string. It returns the leftmost index of the location where the substring is located. Returns 1 if it is not found.
>>> name= "my Name" is peace and is " #查找的是最左端的 >>> Name.find ( ' is ' ) 8 # No return-1 >>> name.find ( ' one ' )- 1 #可以提供查找的起点, starting from 9, look at >>> name.find ( ' is ' , 9 ) 21 #当然可以同时给出终点和起点 >>> name.find ( ' is ' , 10 , 11 )-1 Span class= "hljs-prompt" >>>>
Join method and Split method
The Join method is used to concatenate elements in a sequence (note that all must be strings), and split is the opposite, which is used to split the string into sequences
#如果seq =[1,2,5,6] will be an error .>>>seq=[' 1 ',' 2 ',' 5 ',' 6 ']>>>Adhere='-'>>>Adhere.join (seq)' 1-2-5-6 '>>>Adhere'-'>>>A=adhere.join (seq)>>>A' 1-2-5-6 '>>>A.split ('-')[' 1 ',' 2 ',' 5 ',' 6 ']>>>
Lower method
The lower method returns the lowercase master of the string. Very useful for finding, see the following example:
>>> name=‘ONE Peace‘>>> k=name.lower()>>> k‘one peace‘>>> ‘ONE Peace‘in [‘one peace‘,‘nick name‘]False>>> ‘ONE Peace‘in [‘one peace‘,‘nick name‘]True>>>
Replace method and Translate method
The Replace method returns a string after all occurrences of a string have been replaced.
The Translate method, like the Replace method, can replace some parts of a string, but unlike the one, the translate method handles only a single character. Its advantage is that multiple substitutions can be made at the same time, sometimes much more efficient than replace. Before you can use the translate conversion, you need to complete a conversion table first. The conversion table is a corresponding relationship that replaces a character with a character. Sometimes the table is longer, we still do not write their own, directly with the Maketrans method on the line. The Maketrans method receives two parameters: two equal-length strings, which indicate that each character in the first string is replaced with a character in the same position in the second string.
It is important to note that the previous version of the python3.0 Maketrans function needs to be introduced in the string
>>>st=' is was is '>>>St2=st.replace (' is ',' Peace ')>>>St2' Peace Peace Peace Peace Peace '>>>st=' ABCDEFGHIJKLM '>>>Table=st.maketrans (' BC ',' AA ')>>>Len (table)2>>>table{98: the, About: the}>>>St.translate (table)' AAADEFGHIJKLM '
Strip method
The Strip method returns a string that strips both sides (with no interior) space (or a specified character)
>>> st=‘ aaaaa ‘>>> st.strip()‘aaaaa‘>>> st=‘***jjjj***‘>>> st.strip(‘*‘)‘jjjj‘>>>
Get started with strings here;
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
A string of getting started with Python