Skip to content

Python 305 數字反轉

Python String TQC

題目說明:

請撰寫一程式,讓使用者輸入一個正整數,將此數值以反轉的順序輸出。

範例輸入1
31283

範例輸出1

38213

範例輸入2

1003120

範例輸出2

0213001

Solution

a = input()
print(a[::-1])

題目解析

切片是一種Python中用於從序列(例如字串、列表、元組等)中提取子序列的方法。以下是切片的基本用法:

  1. 基本切片:

    sequence[start:stop:step]
    

    - start:起始索引(包含)。
    - stop:終止索引(不包含)。
    - step:步長(選擇性,默認為1)。

  2. 省略參數:

    sequence[start:stop]
    

    如果省略start,則默認為序列的開始;如果省略stop,則默認為序列的結尾。省略step時,默認為1。

  3. 負數索引:

    sequence[-start:-stop]
    

    使用負數索引時,表示從序列的末尾開始計算。

  4. 反向切片:

    sequence[::-1]
    

    使用負的step值可以將序列反轉。

  5. 其他用法:
    - 如果start大於或等於stop,且step為正,則返回空序列。
    - 如果start小於或等於stop,且step為負,則返回空序列。


Last update : 13 novembre 2024
Created : 13 novembre 2024

Comments

Comments