[Python] basic Python strings and basic python strings

Source: Internet
Author: User

[Python] basic Python strings and basic python strings

The syntax of Python is indented. Generally, four spaces are used and case sensitive.

 

Character encoding

 

The computer can only process numbers. To process text, you must convert the text into numbers before processing.

8 bits are used as one byte)

The maximum integer for saving a word is 255 (Binary 11111111 = decimal 255)

 

Encode 127 letters into one ASCII code. A character is A byte. For example, A is 65.

Encodes English letters and other languages into a Unicode encoding table. A single character contains two bytes, for example, 20013.

 

Converts English letters and other languages into UTF-8 encoding (Variable Length Encoding). An English character is a byte, and a Chinese character is three bytes.

 

Python string

 

In python3, strings are Unicode encoded and support multiple languages

Use the ord () function to convert A character into A 10-digit integer. For example, print (ord ('A') Outputs 65

Use the char () function to convert a 10-digit integer into a character, for example, print (chr (20013) output.

 

If you want to save the characters on the hard disk or transmitted over the network, you need to convert the characters into bytes.

Call the encode () method of the str object to convert the string into bytes,

For example, print ("taoshihan". encode ("UTF-8") Outputs 'taoshihan'

Print ("Tao shihan". encode ("UTF-8") Outputs B '\ xe9 \ x99 \ xb6 \ xe5 \ xa3 \ xab \ xe6 \ xb6 \ xb5'

Python defines bytes data with a prefix plus B

 

Reading data from the network is a byte stream, using decode ("UTF-8") to convert data into characters

For example, print (B '\ xe9 \ x99 \ xb6 \ xe5 \ xa3 \ xab \ xe6 \ xb6 \ xb5'. decode ("UTF-8") Outputs Tao shihan

 

Use the len () function to calculate the number of characters in a string. For example, len (TAO) Outputs 1.

Add #-*-coding: UTF-8-*-to the top of the file -*-

 

Use the % operator to format the character and replace the variable of the string

Example: info = "I am % s, I have % s block money" % ("Tao shihan", 10000)

Print (info) Output: I'm Tao shihan and I have 10000 yuan

The content in the brackets must correspond to the previous order.

 

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.