<<python Basic Tutorials >> Learning Notes | No. 03 Chapter | String

Source: Internet
Author: User

No. 03 Chapter: Using Strings

------

Supported operations

  • Index
  • Slice
  • add element
  • Delete Element
  • Updating elements
  • Find elements (check if an element is part of a sequence)
  • Sequence length
  • Maximum Value
  • Minimum value
  • Other built-in functions
>>> website= ' http://www.python.org ' >>> website[-3:]= ' com '   #此操作不合法 because the string is immutable and cannot be modified Traceback (most recent):  File "<pyshell#162>", line 1, in <module>    website[-3:]= ' com ' TypeError: ' Str ' object does not support item assignment
------

string formatting:  
%s: Conversion description Fu Yuanzu

#字符型 >>> str1 = "Hello,%s.%s enough for ya?" >>> val1 = (' Jerry ', ' hot ') >>> print str1% Val1hello, Jerry.  Hot enough for ya? #浮点型 >>> format = ' PI with three decimals:%.3f ' >>> from math import pi>>> Print format% pipi with three decimals:3.142note:1. With a list, the sequence is interpreted as a value, and only Ganso and dictionaries can format more than one value of 2. Percent semicolon:% template string # Similar to variable substitution in Linux, Template,substitute (convert parameters to pass) >>> s = Template (' $x, glorious $x! ') >>> S.substitute (x= ' Slurm ') ' slurm,glorious slurm! ' #如果是单词的一部分的话, enclosed in {} >>> s = Template (' I love ${x}rry! ') >>> S.substitute (x= ' She ') ' I love sherry! ' #如果是美元, note that $$ is used to represent the dollar symbol, Safe_substitute does not error >>> s = Template (' Using $$ buy ${x}! ') due to missing value or dollar symbol >>> S.substitute (x= ' food ') ' Using $ buy food! ' #利用字典提供键/value pairs >>> s = Template (' A $thing must never $action ') >>> d = {}>>> d[' thing ']= ' gentleman ' >>> d[' action ']= ' show his socks ' >>> s.substitute (d) ' A gentleman must never show his sOcks ' >>> s = Template (' I am ${name},${age} years old. ') >>> d = {' name ': ' Jerry ', ' Age ':50}>>> S.substitute (d) ' I am jerry,50 years old. ' #>>> s = '%s plus%s equals%s '% (1,1,2) >>> print s1 plus 1 equals 2
------

Basic Conversion Description Symbols:

(1)% character, beginning of the symbol conversion
(2) Conversion flag (optional)

    • -Align Left
    • + positive and negative numbers before conversion values
    • "" White space character, before a positive number, preserves space
    • 0 if the conversion value is not enough, fill with zeros
(3) Minimum field width (optional):
    • Minimum width with the specified value
    • If it is *, the value is read from the meta-ancestor
(4). Accuracy value (optional)
    • If it is a real number, the precision value represents the following digits
    • If it is a character, the precision value indicates the following maximum width
    • If it is *, the precision value is read from the meta-ancestor
(5) Conversion type


------

------

------

------

------

------

------

------

------

<<python Basic Tutorials >> Learning Notes | No. 03 Chapter | 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.