Python Learning path 13--string 2

Source: Internet
Author: User
Tags printable characters

1 only applicable with string operator 1.1 format operator (%)a Python-style string format operator. String formatting that is only applicable to string types, very similar to the printf () function in C, is in%, and supports all formatting operations of printf (). string formatting conforms to the following:

%c converted to character (ASCII value, or string of length one)

%r takes precedence over string conversions with the repr () function

%s takes precedence over string conversion with the STR () function

%d,%i converted to signed decimal

%o Convert to unsigned octal

%x,%x converted to unsigned hexadecimal (xx represents the case of the converted hexadecimal character)

%e,%e turn into scientific counting method

%f%f to floating-point (fractional part natural truncation)

Shorthand for %g%g %e and%f/%e and%f

% OUTPUT%

Formatting operator Auxiliary directives


python supports input parameters in two formats. The first is the tuple, which is a C printf () style conversion parameter set; The second form of Python support is the dictionary form. In this form, the key is presented as a format string, and the corresponding value as a parameter in the conversion is provided to the format string. Here are some examples:
<span style= "FONT-SIZE:14PX;"  >>>> '%x '% 108 ' 6c ' >>> '%x '% 108 ' 6C ' >>> '% #X '% 108 ' 0x6c ' >>> >>> '%.2f '% 1234.456789 ' 1234.46 ' >>> '%E '% 1234.456789 ' 1.234457E+03 ' >>> '%g '% 1234.456789 ' 1234.46 ' >> > >>> '%+d '% 4 ' +4 ' >>> '%+d '%-4 '-4 ' >>> ' Host:%s\tport:%d '% (' Mars ', ') ' HOST:MARS\TP Ort:80 ' >>> ' ss\t ' ss\t ' >>> # force on ... >>> ' There is% (Howmany) d% (Lang) s quotation Symbols '% {' lang ': ' Python ', ' Howmany ': 3} ' there is 3 Python quotation Symbols ' >>> </span>

1.2 string templates: A simpler alternativein the format of the dictionary, the programmer will inevitably appear missing the conversion type symbol error. For example, use% (lang) instead of% (Lang) s. To ensure that the string is converted correctly, the programmer must explicitly remember the conversion type parameter. The advantage of a modern string template is that it does not have to memorize all the relevant details, but rather uses $ as a shell language. the Template object has two methods, substitute () and Safe_substitute (). The former in the case of a missing key, he will report a Keyerror exception, while the latter in the absence of key, directly intact to the string display.
>>> from string import Template       #导入template对象 >>> s = Template (' There is ${howmany} ${lang} Quotati On Symbols ') >>> >>> print s.substitute (lang= ' python ', howmany=3) there is 3 Python quotation symbols& gt;>> Print S.substitute (lang= ' Python ') Traceback (most recent call last):  File "<stdin>", line 1, in < module>  File "/usr/lib64/python2.7/string.py", line 172, in substitute    return self.pattern.sub (convert, self.template)  File "/usr/lib64/python2.7/string.py", line 162, in convert    

1.3 Primitive string operator (R/R)The purpose of the primitive operators is to deal with the special characters that appear in the string. In the original string, all characters are used directly in the literal sense, and no special or non-printable characters are escaped.

1.4 Unicode string operator (u/u)It is used to convert a callout string or a string containing Unicode characters into a full Unicode string object.
2 built-in functionsCMP ()The built -in CMP () function is compared against the ASCII value of the string

Len ()Len () returns the number of characters in a string.

max () and Min ()max () and Min () return the maximum and minimum values in a string

Enumerate ()
>>> s = ' foobar ' >>> for i,t in enumerate (s): ...     

zip ()

raw_input ()prompts the user for input with the given string and returns the input.
Python does not have a C-style ending character nul, and how many characters you enter will return the number of characters.





Python Learning path 13--string 2

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.