Python: Using built-in functions to convert strings to integers

Source: Internet
Author: User

Use the built-in function raw_input () built-in function, which reads the standard input and assigns the read data to the specified variable. We can use the Int () built-in function to convert a user-entered string to an integer:

>>> user = Raw_input ("Enter Login Name:")

Enter Login Name:root

>>> print "Your Login is:", user

Your Login Is:root

The above example can only be used for text input, and below is an example of entering a numeric string (and converting the string to an integer):

>>> number = Raw_input ("Now Enter a number:") now enter a number:1024

>>> print "Doubling your number:%d"% (int (number) * 2)

Doubling your number:2048

In Python, in addition to the INT function, there are several functions for converting types to each other.

Float: Converts a string or integer to a floating-point number

STR: Converting a number to a string

CHR: Converts ASCII values to ASCII characters

Hex: Converts an integer to a 16-binary string

Long: Converts a string to a long shape

Oct: Converting integers to eight-binary strings

Ord: Converting ASCII strings to ASCII values

Python: Using built-in functions to convert strings to integers

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.