Python Learning Record (vii)------python string

Source: Internet
Author: User

1, for the encoding of a single character, Python provides a ord() function to get the integer representation of the character, the chr() function converts the encoding to the corresponding character:

2. To calculate str how many characters are included, you can use a len() function:

3, the len() function calculates the str number of characters, if replaced bytes , the len() function calculates the number of bytes:

Formatting and placeholder

Use% as a placeholder in Python, including the following types:

%d integral type

%s string

%f floating Point

%x 16 binary integer

Where%d can be preceded by a number or 0 to indicate the number of integers and whether the preceding 0:

%s can convert any type of data to a string

If% is not a placeholder, just a normal string, use percent

Practice

Xiao Ming's score from last year's 72 points to 85 points this year, please calculate the percentage of small Ming performance increase, and use the string format ‘xx.x%‘ to show that only 1 digits after the decimal point

1 # string-related exercises: Xiao Ming's achievements from last year's 72 points to the 85 points this year, 2 # Please calculate the percentage of the increase in Xiao Ming's grade, and display the ' xx.x% ' in string format, leaving only 1 digits after the decimal point 3 old=724 new=855 pre= (new-old)/old6print(PRE)  7print(' Xiao Ming grades improved%.1f%%' % (pre))

Printing results are:

0.18055555555555555 Xiao Ming's grades increased by 0. 2%

Python Learning Record (vii)------python 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.