Home>Article>Backend Development> How to output three-digit numbers in reverse order in python

How to output three-digit numbers in reverse order in python

青灯夜游
青灯夜游 Original
2021-04-16 16:13:22 41550browse

Method: 1. Use list() to store three digits in the list; 2. Use reverse() to reverse the list elements; 3. Use the "int (list name [subscript])" statement to get the reverse The converted numbers are stored in variables a, b, and c; 4. Use the "a*100 b*10 c" statement to splice into three digits in reverse order; 5. Use print() to output.

How to output three-digit numbers in reverse order in python

#The operating environment of this tutorial: Windows 7 system, Python 3 version, Dell G3 computer.

Principle:

  • Use the list() method to list the input three digits in the form of a string

  • Take out the units, tens, and hundreds digits from the list in reverse order

  • When outputting, pay attention to converting the str type to the int type.

#输入number = 123,输出321 number = input('请输入一个三位数:') list1 = list(number) #将输入的三位数的字符串存入列表 list1.reverse() #将列表元素反转 a = int(list1[0]) #取出反转后列表的元素并将其类型转为int类型 b = int(list1[1]) c = int(list1[2]) #也可以不反转列表,直接反着取出列表中的元素 re_number = a*100 + b*10 + c print('%s的反转数是:%s'%(number,re_number)) print('%d的反转数是:%d'%(int(number),re_number))

Related function description

The input() function in Python3.x accepts a standard input data and returns it as string type.

int() function is used to convert a string or number into an integer.

【Related recommendations:Python3 video tutorial

The above is the detailed content of How to output three-digit numbers in reverse order in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn