This article When working on HackerRank 1/30 using python, This is a summary of what I have investigated.
int: Handle integers double: 15 valid digits float: 6 or 7 effective digits string: Handling of string type round: rounding
Data type conversion is called cast. Note that you often forget str when printing numbers with print.
Example)
str="Hacker"
char_list=list(str)
print(char_list)
Then, ["H "," a "," c "," k "," e "," r "]
is output.
So this time,
["H", "a", "c", "k", "e", "r"]
Is displayed as
To Hacker
I want to fix it.
① Use for
str_list = ['python', 'list', 'exchange']
mojiretu = ' '
for x in str_list:
mojiretu += x
print(mojiretu)
Execution result: pythonlistexchange
② Use join
How to use the join function
String ='delimiter'.join (list)
str_list = ['python', 'list', 'exchange']
mojiretu = ','.join(str_list)
print(mojiretu)
Execution result: python, list, exchange
This is a useful way to partially retrieve the elements of a column.
Basic example) It is quoted from @ ycctw1443.
a = [1, 2, 3, 4, 5]
print(a[0: 4])
print(a[: 4])
print(a[-3:])
print(a[2: -1])
Then [1, 2, 3, 4] [1, 2, 3, 4] [3, 4, 5] [3, 4] Is output.
Develop this, You can also "get elements every nth". ʻA [Start position: End position: Slice increment] `.
a = [1, 2, 3, 4, 5]
print(a[:: 2])
print(a[1:: 2])
print(a[::-1])
print(a[1::-1])
result) [1, 3, 5] [2, 4] [5, 4, 3, 2, 1] [2, 1]
input().split()
Use% to get a string that contains variables You can output it concisely.
print("My favorite fruit is%s." %'Apple')
print("My favorite fruit is%s and%s." %('Apple','Orange'))
x = 'Football'
y = 'snow board'
print("what sports do you like,%s and%s."%(x,y))
% s stands for str () You can display integers and decimals as strings, but % d is an integer.
% r is repr () Display the passed value as it is.
You can expand the array.
https://programming-study.com/technology/python-list-join/ https://code-graffiti.com/print-format-with-string-in-python/
Recommended Posts