Python BASICS (5): String, 2015 python

Source: Internet
Author: User

Python BASICS (5): String, 2015 python

A string is the most common type in Python. You can create a quotation mark with characters in it. In Python, single double quotes are used for the same purpose. Python object types do not have character type. They are generally used as single character strings.
A Python string is a direct volume or scalar. The Python interpreter treats a string as a single value and does not include any other Python type. The Python string cannot be changed. The characters in the string can be accessed through the slice operation.
Python has three types of strings, which are generally meaningful strings (str), Unicode strings (unicode), and abstract class strings (basestring ). In fact, the first two are the last child classes. Basestring cannot be instantiated. If you try to instantiate it, you will get the following error message.

>>> basestring('foo')Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: The basestring type cannot be instantiated

Create and assign values to strings
It is easy to create a string. You can create a string directly or use a factory function such as str.

>>> String1 = 'pyhton' >>> string2 = "easy" # single double quotation marks are equivalent >>> string3 = str (123) >>> string4 = str (range (4) >>>> string1 'pyhton' >>>> string2 'easy' >>>> string3 '123' >>> string4' [0, 1, 2, 3]'


Use the direct index or slicing operator to access string characters and substrings

>>> aString = 'Hello World!'>>> aString[0]'H'>>> aString[1:5]'ello'>>> aString[6:]'World!'

 

Change the string and use the value assignment method to "Update" the string.
The same as the numeric type, the string type is also unchangeable. Each update creates a new string.
Delete characters and strings
Because the string is immutable, to delete a character, you can only create a new string.

>>> aString = 'Hello World!'>>> aString = aString[:3] + aString[4:]>>> aString'Helo World!'

 

You can assign an empty string or del statement to delete a string.
In most applications, there is no need to display the delete string

Most operators of a string are sequential operators. refer to the previous notes.
The following is an example of string and member operators.
The string module of Python contains the following predefined strings:

>>> import string>>> string.ascii_uppercase'ABCDEFGHIJKLMNOPQRSTUVWXYZ'>>> string.ascii_lowercase'abcdefghijklmnopqrstuvwxyz'>>> string.ascii_letters'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'>>> string.digits'0123456789'

 

We will use these things to make a small script with a Python valid identifier

import stringalphas = string.letters + '_'nums = string.digitsprint 'Welcome to the Identifier Checker v1.0'print 'Testees must be at least 2 chars long.'inp = raw_input('Indentifier to test?')if len(inp) > 1:  if inp[0] not in alphas:    print 'invalid: first symbol must be alphabetic'  else:    for otherChar in inp[1:]:      if otherChar not in alphas + nums:        print 'invalid: remaining symbols must be alphanumeric'        break    else:      print 'okay as an identifier'

 

Core tips: Performance
In general, from the perspective of performance, it is inefficient to put repeated operations as parameters in a loop.

while i < len(myString):  print 'character %d is:', myString[i]

 

Most operations are wasted on repeated myString calculation. If you save this value, you can loop more efficiently.

length = len(myString)while i < length:  print 'character %d is:', myString[i]

 

In the above example, there are also repeated problems:

for otherChar in inp[1:]:  if otherChar not in alphas + nums:  ...

 

It is inefficient to perform a merge operation every time. You can perform the following operations:

alphnums = alphas + numsfor otherChar in inp[1:]:  if otherChar not in alphas + nums:

 

This script is a textbook routine and is not perfect. First, the length of the identifier must be greater than 1, rather than the Python keyword. To solve these two problems, I wrote the following script:

#! usr/bin/env pythonimport stringimport keywordalphas = string.letters + '_'nums = string.digitsprint 'Welcome to the Identifier Checker v2.0'inp = raw_input('Indentifier to test:')if inp in keyword.kwlist:  print 'It can''t be a keyword.'elif len(inp) > 0:  if inp[0] not in alphas:    print 'invalid: first symbol must be alphabetic'  else:    for otherChar in inp[1:]:      if otherChar not in alphas + nums:        print 'invalid: remaining symbols must be alphanumeric'        break    else:      print 'okay as an identifier'else:  print 'It can''t be None.'

 

Python can use + to connect strings. In addition, there is also a habit of using it.

>>> abc = 'Hello''World'>>> abc'HelloWorld'

 

This method divides the string into several parts for writing and can be used in different rows.
This statement can be mixed with two quotation marks.
When you connect a common string to a Unicode string, it is converted to a Unicode string.

 

Only applicable to string Operators

1. Format operator %

Format characters Conversion Method
% C Convert to a character (ASCII value, or a string of the same length)
% R Use the repr () function to convert strings first
% S String Conversion using the str () function is preferred.
% D/% I Convert to a signed decimal number
% U Convert to unsigned decimal number
% O Convert to the unsigned octal number
% X/% X (Unsigned) to an Unsigned hexadecimal number (x/X indicates the case of the converted hexadecimal character)
% E/% E Convert to scientific notation (e/E control output e/E)
% F/% F Convert to a floating point number (partial decimal truncation)
% G/% G Abbreviation of % e and % f/% E and % F
% Output %


This is only applicable to string operators. It is very similar to the string formatting of printf () in C language, including symbols.
There are also the following auxiliary commands for formatting operators:

Symbol Function
* Define the width or decimal point Precision
- Align left
+ Displays the plus sign (+) before a positive number)
<Sp> A space is displayed before a positive number.
# Display zero ('0') before the octal number and '0x 'or '0x' before the hexadecimal number (depending on whether 'X' or 'X' is used ')
% '%' Outputs a single '%'
(Var) Ing variables (Dictionary parameters)
M. n M is the minimum total width of the display, and n is the number of digits after the decimal point (if available)


Example

>>> '%x'%108'6c'>>> '%X' %108'6C'>>> '%#X' % 108'0X6C'>>> '%f' % 1234.567890'1234.567890'>>> '%.2f' % 1234.567890'1234.57'>>> '%E' % 1234.567890'1.234568E+03'>>> '%e' % 1234.567890'1.234568e+03'>>> '%g' % 1234.567890'1234.57'>>> '%G' % 1234.567890'1234.57'>>> '%e' %(1111111111111)'1.111111e+12'>>> '%+d' % 4'+4'>>> '%+d' % -4'-4'>>> 'we are at %d%%' % 100'we are at 100%'>>> 'Your host is: %s' %'earth''Your host is: earth'>>> 'Host: %s\tPort: %d' % ('mars', 80)'Host: mars\tPort: 80'>>> num = 13>>> 'dec: %d/oct: %#o/hex: %#X' % (num, num, num)'dec: 13/oct: 015/hex: 0XD'>>> "MM/DD/YY = %02d/%02d/%d" % (2, 15, 67)'MM/DD/YY = 02/15/67'>>> w, p = 'Web', 'page'>>> 'http://xxx.yyy.zzz/%s/%s.html' % (w, p)'http://xxx.yyy.zzz/Web/page.html'

 

The string formatting operator is also a debugging tool. All Python objects have a string representation.
The print statement automatically calls str () for each object ()

2. String Template
The disadvantage of a string is that it is not so intuitive. For example, the conversion type symbol is missing during dictionary conversion. To ensure correct conversion, you must remember the conversion type parameters.
The advantage of the new string template is that you do not need to remember all the relevant details. Instead, it uses the dollar sign ($)
The Template object has two methods: substitue () and safe_substitue (). the former is rigorous. when the key is missing, it will report a KeyError exception. when the latter is missing, it will directly display the string intact.

>>> from string import Template>>> s = Template('There are ${howmany} ${lang} Quotation Symbols')>>> print s.substitute(lang='Python', howmany=3)There are 3 Python Quotation Symbols>>> print s.substiture(lang='Python')Traceback (most recent call last):File "<pyshell#3>", line 1, in <module>print s.substiture(lang='Python')AttributeError: 'Template' object has no attribute 'substiture'>>> print s.safe_substitute(lang='Python')There are ${howmany} Python Quotation Symbols

 

3. original string operators (r/R)
Some characters are special escape characters, which can be difficult to print directly.
Therefore, Python provides the original string. In the original string, all characters are literal and have no other meaning.

>>> '\n''\n'>>> print '\n'>>> r'\n''\\n'>>> print r'\n'\n

 


4. Unicode string operator (u/U)
The usage is the same as that of the original character operator to convert a standard string object to a Unicode String object.

Built-in functions
Cmp () of standard built-in functions is not described in detail
Sequence Functions
Len ()
Max () and min ()
Enumerate ()
Zip ()
Apart from the zip () function, I have mentioned it in my previous blog. The functions of zip () are as follows:

>>> s, t = 'foa', 'obr'>>> zip(s, t)[('f', 'o'), ('o', 'b'), ('a', 'r')]


String Functions
Raw_input ()
Prompt the user with the given string and return the input.
Str () and unicode ()
Not repeated
Chr (), unichr () and ord ()
Chr () is an integer parameter ranging from 0 to 255. A corresponding string is returned.
Unichr () is a Unicode Character returned and has a larger range.
Ord () is the pairing function of chr (). ascii parameters are returned for characters.


To be continued

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.