Article directory
- (1) Title description
- (2) Python3 implementation
- (3) Detailed explanation of knowledge points
(1) Title description
(2) Python3 implementation
print(input()[::-1])
(3) Detailed explanation of knowledge points
1. input(): Get the input of the console (in any form). The output is all string type.
str1 = input()
print(str1)
print('提示语句:', str1)
print(type(str1))
'''
asd123!#
提示语句: asd123!#
<class 'str'>
'''
Commonly used forced transfer types | illustrate |
---|---|
int(input()) |
Forced to an integer (the input must be an integer) |
list(input()) |
Coerce to list (input can be of any type) |
1.1. The difference between input() and list(input()) and their mutual conversion methods
- Same point: Both methods can perform a for loop iteration to extract characters, and both are string types after extraction.
- difference:
str = list(input())
Convert the input string to list type, and related operations can be performed. like:str.append()
- Convert list to string:
str_list = ['A', 'aA', 2.0, '', 1]
- method one:
print(''.join(str))
- Method Two:
print(''.join(map(str, str_list)))
Remark:If the list contains numbers, it cannot be directly converted into a string, otherwise the system reports an error.
- method one:
print(''.join([str(ii) for ii in str_list]))
- Method 2:
print(''.join(map(str, str_list)))
map(): Map the specified sequence according to the given function. That is, the incoming function is applied to each element of the sequence in turn, and a new sequence is returned.
(1) Example: If the list contains numbers, it cannot be directly converted into a string, otherwise the system will report an error.
str = ['25', 'd', 19, 10]
print(' '.join(str))
'''
Traceback (most recent call last):
File "C:/Users/Administrator/Desktop/test.py", line 188, in <module>
print(' '.join(str))
TypeError: sequence item 3: expected str instance, int found
'''
(2) Example: If the list contains numbers, convert all elements in the list to strings.
str_list = ['A', 'aA', 2.0, '', 1]
print(''.join(str(ii) for ii in str_list))
print(''.join([str(ii) for ii in str_list]))
print(''.join(map(str, str_list))) # map():根据给定函数对指定序列进行映射。即把传入函数依次作用到序列的每一个元素,并返回新的序列。
'''
AaA2.01
AaA2.01
AaA2.01
'''
2. print(): print output.
[Python] usage of print() function
x, y = 1, 9
print('{},{}' .format(x, y)) # 打印方法一
print('*'*10) # 打印分割符
print(x, ',', y) # 打印方法二
'''
1,9
**********
1 , 9
'''
3. String str(): Convert the parameter to a string type (forcible conversion) - a string is an ordered immutable sequence.
Function description:
str(x, base=10)
Generally speaking, ordered sequence types areSupports indexing, slicing, addition, multiplication, member operations。
print('返回空字符串:', str())
print('整数转换为字符串:', str(-23))
print('浮点数转换为字符串:', str(1.3e2))
print('返回空字符串:', type(str()))
print('整数转换为字符串:', type(str(-23)))
print('浮点数转换为字符串:', type(str(1.3e2)))
print('列表转换为字符串:', str([12, '-23.1', 'Python']))
print('元组转换为字符串:', str((23, '9we', -8.5)))
print('字典转换为字符串:', str({
'Huawei': 'China', 'Apple': 'USA'}))
print('集合转换为字符串:', str({
'China', 'Japan', 'UK'}))
'''
返回空字符串:
整数转换为字符串: -23
浮点数转换为字符串: 130.0
返回空字符串: <class 'str'>
整数转换为字符串: <class 'str'>
浮点数转换为字符串: <class 'str'>
列表转换为字符串: [12, '-23.1', 'Python']
元组转换为字符串: (23, '9we', -8.5)
字典转换为字符串: {'Huawei': 'China', 'Apple': 'USA'}
集合转换为字符串: {'China', 'UK', 'Japan'}
'''