反向字符串切片,第一个字母原样

时间:2018-10-15 14:58:35

标签: string python-3.x

我对反向字符串切片感到困惑。我的意思是我们将字符串存储在c变量中,因此以相反的顺序写入它就像c [::-1]会反转该字符串。但是我想知道如果我希望输入的字符串的第一个字母保持原样。

1 个答案:

答案 0 :(得分:1)

a[start:end:step] # start through not past end, by step

a[:1] # take first character
a[-1:0:-1] # start from end and take until first character in reverse

a[:1] + a[-1:0:-1] # what you want