[英]how to display digits in a number printed, separated by two spaces? in python
Takes a number between 1 – 32767 from the user and passes the number to a function (print_value) as argument.从用户那里获取一个介于 1 – 32767 之间的数字,并将该数字作为参数传递给 function (print_value)。 The function will display the digits in the number separated by two spaces.
function 将显示数字中的数字,以两个空格分隔。 For example, if the user enters an integer, 3467;
例如,如果用户输入一个 integer,3467; The function should print 3 4 6 7.
function 应打印 3 4 6 7。
One way would be to convert the integer to a string and then use .join() to join the characters of a string together with specified characters in between.一种方法是将 integer 转换为字符串,然后使用.join()将字符串的字符与中间的指定字符连接在一起。
def print_value(number):
print(' '.join(str(number))) # converts number to a string and inserts two spaces between each character (' ')
This works because.join() is used to join together the elements of a list into a string.这是有效的,因为 .join() 用于将列表的元素连接到一个字符串中。 Strings can also be interpreted as a list of individual characters ('hi' = ['h', 'i']), so using.join() on them works as well.
字符串也可以解释为单个字符的列表('hi' = ['h', 'i']),因此在它们上使用.join() 也可以。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.