Summary of common string operations in python

Python has rich and powerful libraries. Often nicknamed the glue language, it can easily link together various modules made in other languages ​​(especially C/C++). A common application situation is to use Python to quickly generate the prototype of the program (sometimes even the final interface of the program), and then rewrite the parts with special requirements [2] in a more suitable language. ''' print str2





















str3 = '''Python has a rich and powerful library. It is often nicknamed the glue language, and
can easily link together various modules made in other languages ​​(especially C/C++).
A common application situation is to use Python to quickly generate the prototype of the program (sometimes even the final interface of the program),
and then rewrite the parts with special requirements [2] in a more suitable language. \
'''
print str3
# The output is too long and I won't show it here, please run it and feel the difference between str2 and str3
print '-'*70



print 'E:\note\Python.doc' #\nIt will be treated as Escape symbol processing
# result:E:
# ote\Python.doc
print r'E:\note\Python.doc' #R is added before the string, so special characters are invalid
# result:E:\note\Python.doc
print '-'*70



str4 = 'String\t'
str5 = 'is powerful'
str4 = str4+str5
# No error will be reported, in fact, this is not to modify str4, but to delete the existing str4, and then use '+' to return the new merged string to recreate str4
print str4
# result:

format_str1 = 'Thaere are %d apples %s the desk.' # %d is an integer and %s is a string
a_tuple = (2,'on')
print format_str1 % a_tuple
# result:There are 2 apples on the desk.
format_str2 = 'There are {0} apples {1} the desk.'.format(3,'on')
print format_str2 # This is another way of writing it, which is easier
# result:There are 3 apples on the desk.
print '- '*70



print '''String method'''
str6 = "Zootopia"
print str6.find('to')# Return the index of the first to, note str6[3]='t',str6[4] ='o'
# result: 3
print '-'*70
str6_2 = "Z ootopi a"
print str6_2.split() # Separate characters with spaces
# result: ['Z', 'o', 'o' , 't' , 'o', 'p', 'i', 'a']
print ''.join(str6_2.split()) # It can be restored again through the join function
# result: Zootopia
print '-'*70
str7 = '54321'
print '>'.join(str7) # The previous example is the join of the list, this is the join of the string, the function is similar
# result: 5>4>3>2>1
print '-'*70
str8 = '"Yes!",I answered.'
print str8.split(',')# split() can specify a character as a delimiter
# result:['"Yes!"', ' I answered.']
# If you want to use multiple characters as separators, you can use the following method
sep=['!',',','.','"']
for i in sep:
    str8 = str8.replace( i,' ')# Replace all delimiters with spaces
print str8.split() # Successfully extract all words in the sentence
# result:['Yes', 'I', 'answered']
print '-'*70
str9 = 'A apple'
print str9.count('A' ) #Pay attention to case sensitivity
# result: 1
str9 = str9.lower()
print str9.count('a')
# result: 2
print '-'*70
str10 = '12345'
print str10.isalnum()
# result: True
print '-'*70



print '''Unicode string'''
unicode_str = u'\u4f60\u597d'
print unicode_str #"Hello" Unicode encoding
# result: hello
utf8_str = unicode_str.encode('utf-8')
print utf8_str
# Note that the utf-8 encoding of 'hello' is '\xe4\xbd\xa0\xe5\xa5\xbd' (in the Python Shell Direct input of utf8_str will display this code),
# But the print() function will not automatically decode, so the direct output is garbled
print utf8_str.decode('utf-8')
# result: Hello
print unicode(utf8_str,'utf-8 ') #these two methods are the same
# result: hello

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326767415&siteId=291194637