Python Learning Notes-python string and formatting

Source: Internet
Author: User
Tags ord

Because Python was born earlier than the Unicode standard, the earliest Python only supported ASCII encoding, and ordinary strings were ‘ABC‘ ASCII-encoded inside python. Python provides the Ord () and Chr () functions to convert letters and corresponding numbers to each other:

>>> ord(‘A‘)65>>> chr(65)‘A‘

Python later added support for Unicode, expressed in Unicode as a string u‘...‘ , for example:

>>> printu‘中文‘中文>>> u‘中‘u‘\u4e2d‘

Written u‘中‘ and u‘\u4e2d‘ is the same, \u is followed by a hexadecimal Unicode code. So, u‘A‘ and u‘\u0041‘ the same.

How do two strings convert to each other? Although the string ‘xxx‘ is ASCII encoded, it can also be seen as UTF-8 encoding, and u‘xxx‘ only Unicode encoding.

u‘xxx‘how to convert to UTF-8 encoding ‘xxx‘ encode(‘utf-8‘) :

>>> u‘ABC‘.encode(‘utf-8‘)‘ABC‘>>> u‘中文‘.encode(‘utf-8‘)‘\xe4\xb8\xad\xe6\x96\x87‘

The UTF-8 value represented by the English character conversion is equal to the Unicode value (but with a different storage space), and the Chinese character converts 1 Unicode characters into 3 UTF-8 characters, and you see one of \xe4 them because its value is 228 , No corresponding letters can be displayed, so the numeric value of the bytes is displayed in hexadecimal. len()the function can return the length of the string:

>>> len(u‘ABC‘)3>>> len(‘ABC‘)3>>> len(u‘中文‘)2>>> len(‘\xe4\xb8\xad\xe6\x96\x87‘)6

In turn, convert the UTF-8 encoded string ' xxx ' to a Unicode string u‘xxx‘ using the decode (' Utf-8 ') method:

>>> ‘abc‘.decode(‘utf-8‘)u‘abc‘>>> ‘\xe4\xb8\xad\xe6\x96\x87‘.decode(‘utf-8‘)u‘\u4e2d\u6587‘>>> print‘\xe4\xb8\xad\xe6\x96\x87‘.decode(‘utf-8‘)中文

Formatting of Python

The last common question is how to output a formatted string. We often output strings that are similar, ‘亲爱的xxx你好!你xx月的话费是xx,余额是xx‘ and XXX's content varies by variable, so a simple way to format a string is required.
In Python, the format used is consistent with the C language and is implemented as a%, for example:

>>> ‘Hello, %s‘‘world‘‘Hello, world‘>>> ‘Hi, %s, you have $%d.‘ % (‘Michael‘1000000)‘Hi, Michael, you have $1000000.‘

As you may have guessed, the % operator is used to format the string. Inside the string, the representation is replaced by a string, which %s %d is replaced with an integer, there are several %? placeholders, followed by a number of variables or values, the order to correspond well. If there is only one %? , the parentheses can be omitted.

Common placeholders are:

%d  整数%f  浮点数%s  字符串%x  十六进制整数

where formatted integers and floating-point numbers can also specify whether to complement 0 and the number of digits of integers and decimals:

>>> ‘%2d-%02d‘ % (31)‘ 3-01‘>>> ‘%.2f‘3.1415926‘3.14‘

If you're not sure what to use, %s it'll always work, and it will convert any data type to a string:

>>> ‘Age: %s. Gender: %s‘ % (25True)‘Age: 25. Gender: True‘

For Unicode strings, the usage is exactly the same, but it is best to ensure that the substituted string is also a Unicode string:

>>> u‘Hi, %s‘u‘Michael‘u‘Hi, Michael‘

Sometimes, what happens if the% inside the string is a normal character? This time it needs to be escaped and used %% to represent a%:

>>> ‘growth rate: %d %%‘7‘growth rate: 7 %‘

Python learning note-python string and formatting

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.