Python is the preferred IT people do what string handling skills

Python is the first choice for IT professionals do? What are string-handling skills? Getting Started with Python is simple, powerful, is the first choice of people to join the IT development, for faster entry, many people choose to participate in professional learning. Then, thousands of Feng Xiao Bian gave you share knowledge about Python string handling.

 

Python is the preferred IT people do what string handling skills

 

 

Python string is the most commonly used data types, Python requires string must be enclosed in quotes, also use single quotes row, double quotation marks, as long as both sides of the pair of quotation marks can. Python string handling, parsing the data reptiles, cleaning large text data and document processing and other general aspects of the application is very extensive.

Python string processing method as follows:

A string of multiplication and sliced

1) is multiplied. When we write Python code to the separator, this time with the multiplication operation of the string easily achieved.

line='*'*30

print(line)

>>******************************

2) Slice

str='Monday is a busy day'

print (str [0: 7]) # denotes the seventh to take the first string

print (str [-3:]) # denotes character strings from the start to the end of characters antepenultimate

print (str [::]) # Copy String

Second, connect and merge a string

1) is connected, two characters can easily through the "+" connected

str1='Hello'

str2='World'

new_str=str1+str2

print(new_str)

>>>HelloWorld

2) combined, the join method

url=['www','python','org']

print('.'.join(url))

>>>www.python.org

Third, the division of the string

1) ordinary division, with split function, but split can only do simple division, but does not support multiple separated.

phone='400-800-800-1234'

print(phone.split('-'))

>>['400', '800', '800', '1234']

2) complex segmentation, r said they did not escape, separator can be ";" or "," followed by a space or more than 0 extra space, and then follow this pattern to split.

line='hello world; python, I ,like, it'

import re

print(re.split(r'[;,s]\s*',line))

>>>['hello world', 'python', 'I ', 'like', 'it']

Fourth, string search and match

1) Find a general

Find ways to use easily inside look at the long string substring, it returns the index position of the character string, or -1 if not found

str1 = "this is string example....wow!!!"

str2 = "exam"

print(str1.find(str2)) # 15

print(str1.find(str2, 10)) # 15

print(str1.find(str2, 40)) # -1

2) complex matching, we need to use regular expressions.

mydate='11/27/2016'

import re

if re.match(r'\d+/\d+/\d+',mydate):

print('ok.match')

else:

print('not match')

>>>ok.match

Fifth, the replacement string

1) conventional replacing, by the method can replace the

text='python is an easy to learn,powerful programming language.'

print(text.replace('learn','study'))

>>>python is an easy to study,powerful programming language.

2) replacement of complex, the need to use re module sub function

students='Boy 103,girl 105'

import re

print(re.sub(r'\d+','100',students))

>>>Boy 100,girl 100

Sixth, get rid of some specific character string

1) to a space, such as reading a line from the file of text processing time and space necessary to remove each row, table or line breaks.

str = ' python str '

print(str)

# Go and trailing spaces

print(str.strip())

# Spaces to the left

print(str.lstrip())

To the right of the spacebar #

print(str.rstrip())

2) complex text cleanup, you can use str.translate.

Construction of such a first conversion table, the translation table is a table showing the "to" transfer uppercase "TO", which is then removed in old_str '12345', and then through the rest of the string table translation.

instr = 'to'

outstr = 'TO'

old_str = 'Hello world , welcome to use Python. 123456'

remove = '12345'

table = str.maketrans(instr,outstr,remove)

new_str = old_str.translate(table)

print(new_str)

>>>HellO wOrld , welcOme TO use PyThOn. 6

Seven, count the number of strings in a character appears

str = "thing example....wow!!!"

print(str.count('i', 0, 5)) # 1

print(str.count('e')) # 2

Of course, knowledge of Python string handling, there are many in this small series do not see them here. If you want to quickly learn Python technology, select professional learning is very good. You can make both theoretical and practical, do enterprises need talent!

Guess you like

Origin www.cnblogs.com/qianfengzz/p/11592023.html