Python topic 3 basics of strings

  • 2020-05-27 06:03:14
  • OfStack

The most important data types in Python include strings, lists, tuples, dictionaries, etc. This article focuses on the basics of strings in Python.

1. Basics of strings

A string is a collection of 1 ordered character sequences, enclosed in single, double, and triple (both single and double) quotation marks. For example:

s1='www.csdn.NET' s2="www.csdn.Net" s3='''aaabbb'''

The string also includes:

1. Escape strings

For example, the C language defines 1 letter before "\" to represent the common ASCII characters that cannot be displayed, and python also has escape characters, as follows:

\\- backslash symbol \'- single quotation mark \"- double quotation mark \ a- ringing bell \b- backspace (Backspace)

\n- line feed \r- carriage return \f- page change \v- vertical TAB \t- horizontal TAB \e- escape

\000- empty \ oyy-8 character represented by yy \ xyy-10 character represented by yy

2. raw string

In Python, the original string (raw strings) and r turn off the escape mechanism.


# Escape characters and raw character  
s1="aa\nbb" 
print s1 
s2=r"aa\nbb" 
print s2 
 
# The output  
aa 
bb 
aa\nbb 
 
#raw The original string handles the disk path  
open(r'C:\temp\test.txt','a+') 
open('C:\\temp\\test.txt','a+') 

3. unicode string

Tell Python Unicode coding, Unicode (series 1 yards and unicode) is 1 character encoding used by the computer. Use before Unicode ASCII yards, Unicode by using one or more bytes to represent one character. Python default inside all of the string literal use ASCII code, can be in front of the string plus a 'u' statement Unicode string prefix, the 'u' prefix told Python the back of the string to plait Unicode string. Example: ES65E N = u 'aa \ nbb'

Chinese processing 1 is a headache, recommended :Unicode and Python Chinese processing

4. Format a string

String formatting features using string formatting operator % (percent), placed in the top left of the % 1 string format (string), and the right place hope the formatted value, but also a tuple and dictionary. If you need to include percent in a string, using the % %. If the right is a tuple, that each one will be separate formatting elements, each value corresponding to a conversion specifiers. Example:

"your age %d,sex %s,record %f"%(28,"Male",78.5)

Output :'your age 28,sex Male,record 78.500000'

It is somewhat similar to printf("%d",x) in C language, where the percent sign % corresponds to the comma in C language, where the string format conversion type is as follows:

d,i signed decimal integers

o unsigned base 8

u unsigned hexadecimal

x unsigned base 106 (lowercase)

X unsigned base 106 (uppercase)

Floating point Numbers (lowercase, uppercase) represented by the scientific notation e,E

f,F decimal floating point Numbers

c single character

r string (any Python converted using repr)

s string (any Python converted using str)

g,G index greater than 4 or less than the precision value is the same as e, otherwise it is the same as f

2. String operation

The basic operations of a string include segmentation, indexing, multiplication, membership, length, and so on.

1.+ connection operation

Such as: s1 = 'csdn s2 =' Eastmount 's3 = s1 + s2

print s1,s2 = > Output: csdn Eastmount
print s3 = > Output: csdnEastmount

2.* repeat operation

Such as: s1 = 'abc * 5

print s1 = > Output: abcabcabcabcabc

3. The index s [index]

The index format of Python, string_name[index], can access the character members in the string.

4. Slice s [i: j]

The basic format of slices in Python is s[i:j:step], where step represents the direction of slices, starting from 0 is not written, and ending from 0 to the end is not written. For example:


  s='abcdefghijk'
  sub=s[3:8] 
  print sub =>  The output defgh_
      3 78 ( The starting point is 3  At the end of 8 Don't take )

Where, when step=-1, it represents the slice in the opposite direction, such as:


  s='abcdefghijk'
  sub=s[-1:-4:-1]
  print sub =>  The output kji

Since the last "-1" means slice from the opposite direction,s[9]=' s[-2]='j', the first 'a' index value is 0, and the last 'k' index value is -1. So 'j' is -2, and sub[-1:-4:-1] means cut from k(-1 :-4) to h(-4).

If you want to reverse the sequence,s=' www.baidu.com ', s1=[-1::-1]. The starting point is m(-1), and no end point means cut to the end.

5. Field width and precision

This knowledge is involved in the format() function described earlier. For example, '%6.2f'%12.345678 outputs "port 12.35", where 6 represents the field width and 2 represents the precision. Therefore, 1 space is added.

At the same time, zero (0) indicates that the number will be filled with zero, the minus sign (-) is used to align the value to the left, and white space (" ") means a space before a positive number, which is very useful for positive and negative Numbers at the time, plus means that both positive and negative Numbers are marked, and alignment is also useful.


# Field width and precision  
num = 12.345678 
s1 = '%6.2f'%num 
print s1 
# supplement 0 
s2 = '%08.2f'%num 
print s2 
# The minus sign aligns to the left  
s3 = '%-8.2f'%num 
print s3 
# blank  
print ('% 5d'%10) + '\n' + ('% 5d'%-10) 
# symbol  
print ('%+5d'%10) + '\n' + ('%+5d'%-10) 
 
# The output  
 12.35 
00012.35 
12.35 
 10 
 -10 
 +10 
 -10 

3. String method

Strings "inherit" many methods from the string module. Here are some common methods:

find()

Find the substring in a long string, and it returns the leftmost index of the location of the substring. If it is not found, it returns -1. The format is "sub [,start [,end]] -" > int", where the method accepts optional start and end point arguments, while rfind() looks from right to left.


title = 'Hello Python,Great Python' 
length = len(title) 
print length 
print title.find('Python') 
print title.find('Python',10,30) 
 
# The output : 
25 
6 
19 

join()

The format is "S.join(iterable) -" > string", meaning "Return a which the concatenation the iterable The elements elements is S." is used to add elements to a queue, but the elements in the queue must be strings. It is the inverse of the split method.


seq = ['1','2','3','4'] 
sep = '+' 
print sep.join(seq) # Concatenation string list  sep said '+' The connection  
 
dirs = '','usr','bin','env' 
print '/'.join(dirs) 
print 'C:'+'\\'.join(dirs) 
 
# The output  
1+2+3+4 
/usr/bin/env 
C:\usr\bin\env 

split()

String segmentation function in the form of "S.split ([sep [,maxsplit]]]) - > list of strings", splits a string into a sequence. If you do not provide a separator, the program will use all Spaces as delimiters.


# Split by space 4 A word , return list 
s = 'please use the Python!' 
li = s.split() 
print li 
print '1+2+3+4+5'.split('+') 
# The output  
['please', 'use', 'the', 'Python!'] 
['1', '2', '3', '4', '5'] 

strip()

S.strip ([chars]] removes the specified characters by removing the space bar at the beginning and end of the string (both sides and not inside), while the function lstrip() removes all Spaces at the beginning of the string and rstrip() removes all Spaces at the end of the string.

replace()

This method returns a string where all matches have been replaced, as in the "find and replace" function of a word processor.

translate()

This method, like replace1, can replace a part of a string, but the difference is that translate only handles a single character. Its advantage is that it can replace multiple characters at the same time, sometimes more efficiently than replace.

Such as: s = 'eastmount s1 = s. replace (' e', 'E') = > After the replacement 'Eastmount'

String judgment method

isalnum() to determine whether all characters are valid (alphanumerics + digits), for example, to determine the password account, output Ture\False.

isalpha() determines if it is a letter

isdigit() determines whether it is a number

islower() determines if it is all lowercase

isupper() determines if it is all uppercase

isspace() determines whether it is a space (")

lower()

This method returns a lowercase version of the string, which is used when determining user names are case-insensitive.upper() is converted to uppercase, and the title() function converts the string to a title -- the first letter of all the words is uppercase, and the rest is lowercase, but it USES a word-division method that can get unnatural results.


s = 'this is a good idea' 
s1 = s.upper() 
print s1 
s2 = s.title() 
print s2 
 
# The output  
THIS IS A GOOD IDEA 
This Is A Good Idea 

PS: I mainly through "Python basic tutorial" and "51 ZhiPu CTO college education of python video" learning. So this paper cites a lot of knowledge, books, and own knowledge of video, thanks to the author and the teacher, hope to be of article, began to study python knowledge, if there are errors or deficiencies article, also please burke, also want you to put forward opinions to share with you. Do not spray ~


Related articles: