Home >Backend Development >Python Tutorial >Python learning basics string processing understanding

Python learning basics string processing understanding

高洛峰
高洛峰Original
2017-03-24 16:58:281436browse

PythonStringProcessing
String input:

my_string = raw_input("please input a word:")


String judgment:
(1) Determine whether it is a pure letter

my_string.isalpha()


String search match:
(1) re
reregular expression Formula Example 1: ^[\w_]*$
First, \w means to match any word character including an underscore, which is equivalent to '[A-Za-z0-9_]'.
Then I followed _.
Look at the * sign again: match the previous subexpression zero or more times. For example, zo* matches "z" and "zoo". * Equivalent to {0,}.
The last is $: it means the end of the string, there are no other characters after it.
So, the meaning of this expression is to treat this [\w_] (any word character including an underscore, followed by an underscore) as a whole, appearing zero or more times!

import re
my_string = raw_input("please input a word:")
if re.match('^[a-zA-Z]$', my_string):
print "it is a word"
else:
print "it is not a word"


String transformation:
(1) Convert the string to all lowercase letters.

my_string = my_string.lower()


(2) Connect multiple strings together.

my_string = my_string + "abc"


(3) Intercept part of the string. This example removes the first and last characters and intercepts the middle section.

my_string = my_string[1:len(my_string)-1]


Thank you for reading, I hope it can help everyone, thank you for your support of this site

The above is the detailed content of Python learning basics string processing understanding. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn