第一期 Python 程式設計入門共學營作業任務五


1. 問答申論題:

今天要跟大家介紹 Python 字串物件的一個方法 str.endswith(suffix[, start[, end]])

以下是官方文件的介紹:

Return True if the string ends with the specified suffix, otherwise return False. suffix can also be a tuple of suffixes to look for. With optional start, test beginning at that position. With optional end, stop comparing at that position.

意思就是如果字符串以指定的後綴結尾,則返回True,否則返回False。 後綴也可以是要查找的後綴元組。 使用可選的啟動,從該位置開始測試。 在可選端,停止在該位置進行比較。以下是一個使用範例:
參數:

  • suffix -- 這可能是一個字符串或者是元組用於查找後綴。
  • start -- 切片從此開始
  • end -- 切片到此為止
    ```
    str = "this is string example....wow!!!";

suffix = "wow!!!";

True

print(str.endswith(suffix))

True

print(str.endswith(suffix,20))

suffix = "is";

True

print(str.endswith(suffix, 2, 4))

False

print(str.endswith(suffix, 2, 6))


# 2. 程式設計實作題:
請設計一個程式:讓使用者可以輸入字串,並透過取出使用者輸入的字串將最後一個字元取出轉成大寫後印出結果。

str = input("請輸入字串:")

最後一個字元取出轉成大寫

strlast = str[-1].upper()

字串長度

strlength = len(str)

除了最後一個字元,取出其他字元

strRemain = str[0: strlength - 1]

newstr = strRemain + strlast
print(newstr)
```

#第一期Python程式設計入門共學營






你可能感興趣的文章

Vue Router-Day01 # 簡介與 在一般網頁中引入Vue Router

Vue Router-Day01 # 簡介與 在一般網頁中引入Vue Router

開啟測試這扇門的鑰匙

開啟測試這扇門的鑰匙

CSS

CSS






留言討論