Python basics document string, python basics tutorial

Source: Internet
Author: User
Tags print format

Python basics document string, python basics tutorial

  • String formatting

String formatting is implemented using the string formatting operator % Percent. Place a string (formatted string) on the left side of %, and put the value to be formatted on the right side. You can use a value, such as a string or number, you can also use tuples or dictionaries with multiple values, such

>>> Print "hello. % s. % s enough for ya? "% ('World', 'hot ')
Hello. world. Hot enough for ya?

If the right operand is a tuples, each element is formatted separately, and each value requires a conversion specifier.

The basic conversion specifiers include the following parts. Note that the order of these items is crucial.

1.% character: mark the start of the conversion specifier.

2. conversion sign (optional):-indicates left alignment; + indicates adding a plus or minus sign before the conversion value; "" indicates retaining spaces before a positive number; 0 indicates that the conversion value is filled with 0 if the number of digits is not enough.

3. Minimum field width (optional): The converted string must have at least the width specified by this value. If it is *, the width is read from the value tuples.

4. Point (.) followed by precision value (optional): If the conversion is a real number, the precision value indicates the number of digits after the decimal point. If the conversion is a string, the number indicates the maximum field width. If it is *, the precision will be read from the tuples.

5. Conversion Type: See the following table.

For example

# Print the formatted price list width = input ('width: ') with the given width :') price_width = 10item_width = width-price_widthheader_format = '%-* s % * s' format =' %-* s % *. 2f 'print '-' * widthprint header_format % (item_width, 'item', price_width, 'price') print '-' * widthprint format % (item_width, 'append', price_width, 0.4) print format % (item_width, 'pears', price_width, 0.5) print format % (item_width, 'cantaloupes', price_width, 1.92) print format % (item_width, 'dried transaction COTS (16 0z .) ', price_width, 8) print format % (item_width, 'prunex (4 1bs)', price_width, 12) print '=' * width

The result is as follows:

Width: 35
-----------------------------------
Item Price
-----------------------------------
Apples 0.40
Pearl 0.50
Cantaloupes 1.92
Dried transaction COTS (16 0z.) 8.00
Prunex (41bs) 12.00
======================================

  • String Method

Find -- find the substring in a long string. It returns the leftmost index of the substring's position. If the substring is not found,-1 is returned. This method also receives optional start and end points.

>>> Title = 'hello. world. Hot enough for ya? '
>>> Title. find ('wor ')
7
>>> Title. find ('wor', 2, 6)
-1

Join -- add an element to the queue

>>> Seq = list ('20140901 ')
>>> Sep = '+'
>>> Sep. join (seq)
'1 + 2 + 3 + 4 + 5'
>>> Dirs = '', 'usr', 'bin', 'env'
>>> '/'. Join (dirs)
'/Usr/bin/env'
>>> 'C: '+' \ '. join (dirs)
'C: \ usr \ bin \ env'

Lower -- returns the lower-case string

Replace -- returns the string obtained after all matching items of a string are replaced.

Split -- Splits a string into a sequence.

>>> '1 + 2 + 3 + 4 + 5'. split ('+ ')
['1', '2', '3', '4', '5']
>>> '/Usr/bin/env'. split ('/')
['', 'Usr', 'bin', 'env']
>>> 'C: \ usr \ bin \ env '. split ('\\')
['C: ', 'usr', 'bin', 'env']

Strip -- returns a string that removes spaces on both sides (excluding internal spaces). You can also specify characters to remove.

>>> '++! ** 1 + 2 + 3 + 4 + 5 * + '. strip (' +! *')
'1 + 2 + 3 + 4 + 5'

Translate -- some parts of the string can be replaced. Only a single character can be processed and multiple replacement can be performed at the same time. The first parameter specifies the table to be replaced, and the second parameter specifies the characters to be deleted.

>>> From string import maketrans
>>> Table = maketrans ('cs ', 'kz ')
>>> 'This is an incredible test '. translate (table)
'Thiz iz an inkredible tezt'

>>> 'This is an incredible test '. translate (table ,'')
'Thizizaninkredibletezt'

 

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.