Basic Python knowledge: string processing and basic python knowledge

Source: Internet
Author: User

Basic Python knowledge: string processing and basic python knowledge

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!

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.