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

Python learning basics string processing understanding

高洛峰
Release: 2017-03-24 16:58:28
Original
1403 people have browsed it

PythonStringProcessing
String input:

my_string = raw_input("please input a word:")
Copy after login


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

my_string.isalpha()
Copy after login


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"
Copy after login


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

my_string = my_string.lower()
Copy after login


(2) Connect multiple strings together.

my_string = my_string + "abc"
Copy after login


(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]
Copy after login


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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template