Как изменить строку в Python
# in order to make a string reversed in python
# you have to use the slicing as following
string = "racecar"
print(string[::-1])
Tejas Naik
# in order to make a string reversed in python
# you have to use the slicing as following
string = "racecar"
print(string[::-1])
'hello world'[::-1]
'dlrow olleh'
#linear
def reverse(s):
str = ""
for i in s:
str = i + str
return str
#splicing
'hello world'[::-1]
string = 'abcd'
''.join(reversed(string))
# To reverse a list use simply: reverse() method
# Example
list = [1, 2, 3]
list.reverse()
# Output
[3, 2, 1]
# to reverse any other type, we can use reversed()
# The reversed() function returns an iterator that accesses the given sequence in the reverse order.
# Example
list = [1, 2, 3]
list(reversed(list))
# Output
[3, 2, 1]
# reversed() returns an iterator so we can loop other it
# Example
for num in reversed(list):
print(num)
# Output
3
2
1
name = "Sharad"
reverse = "" #Empty String.
index = len(name) #index = 6
for ch in name:
reverse = reverse + name[index-1] #indexing strats at zero i.e name[5]=d.
index = index-1
print (name)
print (reverse)