Python Slice and Reverse a String
Given a string, take a specified number of characters from the beginning or the end, then reverse and concatenate them.
Example
def
rotate
(
input
,
d
)
:
Lfirst
=
input
[
0
:
d
]
Lsecond
=
input
[
d
:
]
Rfirst
=
input
[
0
:
len
(
input
)
d
]
Rsecond
=
input
[
len
(
input
)
d
:
]
(
"
Head slice reversed:
"
,
(
Lsecond
Lfirst
)
)
(
"
Tail slice reversed:
"
,
(
Rsecond
Rfirst
)
)
if
name
==
"
main
"
:
input
=
'
Runoob
'
d
=
2
Take two characters
rotate
(
input
,
d
)
The output of the code above is:
头部切片翻转 : noobRu
尾部切片翻转 : obRuno
Python3 Examples