Text processing in the lovely Python:python

Source: Internet
Author: User
Tags processing text in python

Like several other popular scripting languages, Python is an excellent tool for browsing and processing text data. This article outlines Python's text-processing tools for beginners in Python. This article describes some of the general concepts of rule expressions and provides suggestions for how to use (or not use) regular expressions when working with text.

What is Python?

Python is a very advanced interpretive language developed by Guido van Rossum and is available for free. Its syntax is simple and understandable, and its object-oriented semantics are powerful (but flexible). Python can be widely used and highly portable.

Strings--Immutable sequences

As with most advanced programming languages, variable-length strings are the basic type in Python. Python allocates memory in the background to hold strings (or other values) that programmers don't have to worry about. Python also has some string processing capabilities not in other high-level languages.

In Python, strings are "immutable sequences." Although you cannot modify a string, such as a byte group, by location, the program can refer to the element or subsequence of the string as if you were using any sequence. Python uses a flexible "fragment" operation to refer to a subsequence, which is similar in format to a range of rows or columns in a spreadsheet. The following interactive sessions illustrate the use of strings and character fragments:

Strings and fragments

>>> s =
    "mary had a little lamb"
>>> s[0]
    # index is zero-based
    'm'
>>> s[3] =
    'x'
    # changing element in-place fails
Traceback (innermost last):
File
    "<stdin>", line 1,

     in
     ?
TypeError: object doesn't support item assignment
>>> s[11:18]
    # 'slice' a subsequence
    'little '
>>> s[:4]
    # empty slice-begin assumes zero
    'mary'
>>> s[4]
    # index 4 is not included in slice [:4]
    ' '
>>> s[5:-5]
    # can use "from end" index with negatives
    'had a little'
>>> s[:5]+s[5:]
    # slice-begin & slice-end are complimentary
    'mary had a little lamb'

Related Article

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.