Python string processing string input: my_stringraw_input (& quot; pleaseinputaword: & quot;) string judgment: (1) determine whether the string is matched with the pure letter my_string.isalpha (): (1) rere regular expression Example 1: ^ [\ w _] * $ First \ w represents matching any word characters including underscores, equivalent to [A-Za-z0-9 _]. Then followed by _. Check the number * again: match the previous subexpression
Python string processing
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 matching:
(1) re
Re regular expression Example 1: ^ [\ w _] * $
First, \ w indicates matching any word characters including underscores, which is equivalent to '[A-Za-z0-9 _]'.
Then followed by _.
*: Matches the previous subexpression zero or multiple times. For example, zo * can match "z" and "zoo ". * Is equivalent to {0 ,}.
$: Indicates that the string ends and no other characters are followed.
Therefore, this expression refers to the [\ w _] (any word character includes an underscore, followed by an underscore) as a whole, and appears zero or multiple times!
import remy_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 conversion:
(1) convert the string to lowercase letters.
my_string = my_string.lower()
(2) connect multiple strings together.
my_string = my_string + "abc"
(3) part of the string. In this example, the first and last characters are removed and the middle section is truncated.
my_string = my_string[1:len(my_string)-1]
Thank you for reading this article. I hope it will help you. thank you for your support for this site.
The above is the details about string processing based on Python learning. For more information, see other related articles in the first PHP community!