Python Learning Note: python string

Source: Internet
Author: User

Two, the Python string operator 1. Object Standard type operator

There are three types of standard type operators for Python objects: Comparison of object values , object Identity comparison , boolean type . The comparison of the object values is mainly the mathematical comparison of greater than, less than, not equal to, etc., the object identity is mainly the "is" and "is" not the two symbols; the Boolean type is primarily a logical operator of not, and, or.

This is also true of the string standard type operators, which are compared by the size of the ASCII value when doing a comparison operation.

2. Sequence type operators

Slice operator

There are three main types, namely forward index, reverse index and default index. The number of the index displayed in:

Note: Either the start/End index does not specify that the entire string is returned, the index value is specified as none, and it is returned to the beginning or end of the string.

Member operators

In or not in. The member operator determines whether a character or a substring is part of another string, returns True if it occurs, or false.

Write a script idcheck.py to detect Python variables. Tips for some points of knowledge:

>>> import string>>> string.uppercase ' abcdefghijklmnopqrstuvwxyz ' >>> string.lowercase ' ABCDEFGHIJKLMNOPQRSTUVWXYZ ' >>> string.letters ' abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz ' >>> string.digits ' 0123456789 '

This assumes that the Python variable consists of at least two characters.

Code implementation:

#! /usr/bin/pythonimport Stringalphas = string.letters + ' _ ' nums = String.digitsprint ' Welcome to the Identifier Checker v1.0 ' PRINT ' Python variables must is at least 2 chars long ' MYINP = Raw_input (' Identifier to test? ' If Len (MYINP) > 1:if myinp[0] not in alphas:print ' Invalid:first symbol must is alphabetic ' else:for Otherchar in MyI Np[1:]:if Otherchar not in Alphas + nums:print ' invalid:remaining symbols must being alphabetic ' breakelse:print ' okay as an Identifier ' else:print ' Invalid:python variables must is at least 2 chars long '

Here every time in the loop, you have to use the plus (+) string connector, to open up new storage space, so the efficiency is very low. A good solution for improvement:

Alphanums = Alphas + numsfor Otherchar in myinp[1:]:if Otherchar not in Alphanums:

There is also the For-else Loop statement, which is executed only when the For loop is complete and does not encounter a break.

Connector (+)

A new string is obtained from the original string by the join operator.

>>> s = ' Spanish Inquisition Made Easy ' >>> import string>>> string.upper (S[:3] + s[20]) ' SPAM '

The above method has a performance problem because Python must allocate new memory for each string participating in the connection operation, including the resulting new string. So it is recommended to use the string formatting operator (%), or to put all the strings in a class table, and then connect them together with a join () method.

>>> '%s '% (' Spanish ', ' inquisition ') ' Spanish inquisition ' >>> s = '. Join (' Spanish ', ' inquisition ' , ' Made Easy ')) >>> s ' Spanish Inquisition Made Easy ' >>> ('%s%s '% (S[:3], s[20])). Upper () ' SPAM '

Ordinary strings converted to Unicode strings

If you concatenate a normal string with a Unicode string, Python automatically converts the normal string to a Unicode string before the connection operation.

>>> ' Hello ' + u ' + ' world ' +u '! ' U ' Hello world! '
3. Operators that apply only to strings

Formatting operators

Python-style string formatting operators, only for string types, very similar to the C language inside the printf function string formatting.

Python supports input parameters in two formats. The first form is a tuple, and the second form is a dictionary type.

First look at some string formatting symbols and formatting operator auxiliary commands:

Take a look at some specific examples:

>>> '%x '% 108 ' 6c ' >>> '%x '% 108 ' 6C ' >>> '% #x '% 108 ' 0x6c ' >>> '%f '% 1234.567890 ' 1234.5 67890 ' >>> '%.2f '% 1234.567890 ' 1234.57 ' >>> '%e '% 1234.567890 ' 1.234568e+03 ' >>>>> > '%+d '% 4 ' +4 ' >>> '%+d '%-4 '-4 ' >>> ' We is at%d%% '% ' We is at 100% ' >>> ' Host:%s Por T:%d '% (' Mars ', ' host:mars ') ' port:80 ' >>> ' mm/dd/yy =%02d/%02d/%d '% (2, $) ' Mm/dd/yy = 02/15/67 '

The above example uses a tuple as the input parameter, the following is a dictionary type input parameter:

>>> ' There is% (Howmany) d% (Lang) s quotation Symbols '% {' lang ': ' Python ', ' Howmany ': 3} ' There is 3 Python Quotat Ion Symbols '

  

  

  

  

  

  

  

  

Python Learning Note: Python string

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.