<font color=black size=5> Preface </font>
<font color=black size=3> Recently, many kids who have learned the basics asked me how to improve my programming level ? What should I do after learning the basics ? Mingming learned a lot , Do the project but don't know how to get started , In fact, this is too little practice , Only pay attention to learning , But ignore the question , Only continuous practice can improve and consolidate programming thinking and ability !</br></br></br> I happened to see that niuke.com has recently released Python So I had a good experience with my new question bank </br></br> Link address : Cattle from | Python From introduction to practice , Stop talking nonsense and speed up , Or follow the questions below !!!1. Problem description
<font color=green size=4> Enter a string and output it in reverse order </font>- <font color=black size=4> The first way : String slice
- <font color=black size=4> The second way : Use circular conversion and output in reverse order
- <font color=black size=4> such as : Input string
'hello'
, Reverse output 'olleh'
</br></br>
- <font color=#0099ff size=4> After the partners read the problem description , Be sure to practice yourself first , Then go to see the blogger's code and problem-solving ideas , In order to improve their programming level , It all depends on self-consciousness !!!
2. Algorithm ideas
<font color=black size=4>1. Use a loop to subtract the length of the string from the beginning to 0 end <br /><font color=black size=4>2. Every time the circulation inside the body , Add the characters of the corresponding index to the list <br /><font color=black size=4>3. After completing all the cycles , Convert the list to string output 3. Code implementation
The first way to slice
Implementation code :
# Python3 range() It returns an iteratable object ( Type is object ), Not the list type
# range(start, stop[, step])
# Parameter description :
# start: Count from start Start . The default is 0 Start . for example range (5) Equivalent to range (0,5) ;
# stop: Count to stop end , But does not include stop. for example : range (0,5) yes [0, 1, 2, 3, 4] No, 5
# step: step , The default is 1. for example : range (0, 5) Equivalent to range(0, 5, 1)
# range(5, 0, -1): [5, 4, 3, 2, 1]
# The first way to slice
str_info = input(" Please enter the string :")
print(" The output result in reverse order is :", str_info[::-1])
Running results :
Second cycle conversion
Implementation code :
# Python3 range() It returns an iteratable object ( Type is object ), Not the list type
# range(start, stop[, step])
# Parameter description :
# start: Count from start Start . The default is 0 Start . for example range (5) Equivalent to range (0,5) ;
# stop: Count to stop end , But does not include stop. for example : range (0,5) yes [0, 1, 2, 3, 4] No, 5
# step: step , The default is 1. for example : range (0, 5) Equivalent to range(0, 5, 1)
# range(5, 0, -1): [5, 4, 3, 2, 1]
# Second cycle conversion
str_info = input(" Please enter the string :")
str_list = []
for i in range(len(str_info) - 1, -1, -1):
str_list.append(str_info[i])
print("str_list:", str_list)
print(" The output result in reverse order is :", ''.join(str_list))
Code parsing :
range(len(str_info) - 1, -1, -1)
: Get string index subscript
len(str_info) - 1
: The maximum index position of the string , Such as hello
That is to take 4
- The first 1 individual
-1
: Because the left is closed and the right is open , Actual value to 0
- The first 2 individual
-1
: flashback
''.join(str_list)
: List strings ['o', 'l', 'l', 'e', 'h']
Convert to olleh
character string
Running results :