Understanding about string processing based on Python learning

Source: Internet
Author: User
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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.