Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python Slice and Reverse a String

Python Slice and Reverse a String

Document Object Reference Manual Python3 Examples

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

:

]

print

(

"

Head slice reversed:

"

,

(

Lsecond

Lfirst

)

)

print

(

"

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

Document Object Reference Manual Python3 Examples