how to find the reverse of str1 in a function
时间: 2024-02-04 20:04:25 浏览: 26
To find the reverse of a string `str1` in a function, you can follow these steps:
1. Define a function that takes `str1` as an argument.
2. Convert `str1` into a list using the `list()` function.
3. Reverse the list using the `reverse()` method.
4. Convert the reversed list back into a string using the `join()` method with an empty string as the separator.
5. Return the reversed string.
Here's an example code snippet that implements this logic:
```python
def reverse_string(str1):
# convert str1 to a list
str_list = list(str1)
# reverse the list
str_list.reverse()
# convert the list back to a string
reversed_str = ''.join(str_list)
# return the reversed string
return reversed_str
```
You can call this function with a string as an argument to get its reverse string. For example:
```python
reverse_string("hello world")
```
Output:
```
'dlrow olleh'
```