Python str operation method (detailed description), pythonstr

Source: Internet
Author: User

Python str operation method (detailed description), pythonstr

1. str. format (): format the string using the "{}" Placeholder (the index number format and key-value pairs in the placeholder can be used together ).

>>> String = 'python {}, django {}, tornado {}'. format (2.7, 'web', 'tornado ') # correspond to each {} placeholder, enter string> string 'python2. 7. djangoweb, tornadotornado '>>> string = 'python {}, django {}, tornado {}'. format (2.7, 'web') Traceback (most recent call last): File "<pyshell #6>", line 1, in <module> string = 'python {}, django {}, tornado {}'. format (2.7, 'web') IndexError: tuple index out of range >>> string = 'python {0}, django {2}, tornado {1 }'. format (2.7, 'web', 'tornado ') # You can also specify the value to be entered (starting from 0, the subsequent values may not always be used, however, you must ensure that the specified position has a value.) >>> string 'python2. 7. djangotornado, tornadoweb '>>> string = 'python {py}, django {dja}, tornado {tor }'. format (tor = 'tornado ', dja = 'web', py = 2.7) # values can be assigned in the form of key-value pairs> string 'python2. 7. djangoweb, tornadotornado '>>>

2. Use "%" to format the string.

Format a symbol table

 

% C Convert to single character
% R Convert to a string expressed in repr ()
% S Converted to a string expressed by str ()
% D or % I Convert to a signed decimal integer
% U Convert to an unsigned decimal integer
% O Convert to an unsigned octal integer
% X Converts the string to an unsigned hexadecimal integer. The hexadecimal letter is in lowercase.
% X Converts the string to an unsigned hexadecimal integer. The hexadecimal letter is in uppercase.
% E Converts it to a floating point number expressed in scientific notation, where e is expressed in lowercase.
% E Converts it to a floating point number expressed in scientific notation, where E is expressed in uppercase.
% F or # F Convert to floating point
% G Python automatically converts the value to % e or % f based on the number size.
% G Python automatically converts the value to % E or % F based on the number size.
% Output "%"

 

 Auxiliary formatting symbol table

* Defines the width or decimal point Precision
- Left aligned
+ Returns the positive value symbol "+" to a positive number"
<Sp> When the number size is less than m. n, fill in space
# Display 0 before the octal digit, and 0x or 0X before the hexadecimal digit
0 When the number size is less than m. n, use 0 to fill
M. n M is the minimum total width displayed, and n is the number of digits after the decimal point (if available)

The str operation method of the above Python article (detailed description) is all the content that I have shared with you. I hope to give you a reference and support for the help house.

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.