(Basic Python tutorial) learning notes | chapter 03rd | string

Source: Internet
Author: User
Tags print format

(Basic Python tutorial) learning notes | chapter 03rd | string

Chapter 2: Using strings

------

Supported operations

    Index slices add elements to delete elements update element search elements (check whether an element is a member of a sequence) sequence length maximum value minimum other built-in functions
    >>> Website = 'HTTP: // www.python.org '>>> website [-3:] = 'com' # this operation is invalid because the string remains unchanged, traceback (most recent call last) cannot be modified: File"
      
       
    ", Line 1, in
       
        
    Website [-3:] = 'com' TypeError: 'str' object does not support item assignment
       
      
    ------

    String formatting:
    % S: conversion specifier ancestor

    # Stable type> str1 = "Hello, % s. % s enough for you ya? ">>> Val1 = ('Jerry ', 'hot') >>> print str1 % val1Hello, Jerry. Hot enough for you ya? # Floating point type> format = "PI with three decimals: %. 3f ">>> from math import pi >>> print format % piPI with three decimals: 3.142 Note: 1. if a list is used, the sequence is understood as a value. Only the ancestor and dictionary can format more than one value. percent: % Template string # similar to replacing variables in linux, Template, substitute (convert passed parameters) >>> s = Template ('$ x, glorious $ x! ') >>> S. substitute (x = 'slurm')' slurm, glorous slurm! '# If it is part of a word, enclose it with {}> s = Template (' I love $ {x} rry! ') >>> S. substitute (x = 'she')' I love Sherry! '# If it is a dollar, pay attention to the $ symbol, safe_substitute does not report errors due to missing values or dollar signs >>> s = Template ('using $ buy $ {x }! ') >>> S. substitute (x = 'food')' Using $ buy food! '# Use the dictionary to provide key/value pairs >>> s = Template ('A $ thing must never $ Action') >>> d = {}>>> d ['action'] = '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' % (, 2) >>> print s1 plus 1 equals 2
    ------

    Basic conversion specifiers:

    (1) % characters, marking the start of conversion
    (2) Conversion mark (optional)

    -Left alignment + Positive and negative signs before conversion value "" blank character, indicating that the space 0 is reserved before the positive number is not enough, then 0 is used to fill (3) the minimum field width (optional):
    Min width with specified value. If it is *, the value is read from the ancestor (4). accuracy value (optional)
    If it is a real number, the precision value indicates the number of digits after it is a character, the precision value indicates the maximum width after it is *, and the precision value reads (5) the conversion type from the ancestor.


    ------

    Simple Conversion

    >>> print 'price of eggs is %d' % 12price of eggs is 12>>> print 'Hexadecimal price of eggs: %x' % 42Hexadecimal price of eggs: 2a>>> from math import pi>>> 'Pi: %f...' % piPi: 3.141593...>>> 'very inexact estimate of pi: %i' % pivery inexact estimate of pi: 3>>> 'Using str: %s' % 42LUsing str: 42>>> 'Using repr: %r' % 42LUsing repr: 42L
    ------

    Field width and precision
    Both parameters are integers separated by.. If only the precision is given, it must contain a vertex number.

    >>> '% 10f' % pi # The width is 10' 3.141593' >>>' % 10.2f '% pi # The width is 10, the precision is 2 '000000'> '%. 2f '% pi # The precision is 2' 3. 14'> '%. 5s '%' Hello, world! '# String Length 'hello' >>>' %. * s' % (5, 'Hello, World! ') # String Length, whose value reads 'hello' from the ancestor'
    ------

    ------

    ------

    ------

    ------

    ------

    ------

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.