Python 第2章 變數和簡單資料型別

2021-09-11 21:03:04 字數 512 閱讀 9656

2.1 變數的命名和使用

2.2 字串

在python中字串用單引號(' ')或雙引號(" ")所括起

注:在單引號括起的字串中,如果包含撇號,就會導致錯誤。

2.2.1 修改字串的大小寫

a.   title() :以首字母為大寫的方式顯示每個單詞

b.  upper() :將字串全部以大寫字母顯示

c.  lower() :將字串全部以小寫字母顯示

2.2.2 刪除空白

a.  lstrip() :刪除字串開頭的空白

b.  rstrip() :刪除字串末尾的空白

c.  strip() :同時刪除字串兩端的空白

在實際程式中,最常用於在儲存使用者輸入前對其進行清理。

注:刪除只是暫時的,再次訪問時仍會包含多餘的空白。若想永久刪除,需要將刪除操作的結果儲存到變數中。

第2章 變數和簡單資料型別 動手試一試

2 3 個性化訊息 將使用者的姓名存到乙個變數中,並向該使用者顯示一條訊息。顯示的訊息應非常簡單,如 hello eric,would you liketo learn some python today?title 首字母大寫 name liying a hello b eric c would ...

Python 變數和簡單資料型別

message hello python world print message 輸出結果如下 hello python world 1.2 給相同的變數賦值2次或多次的時候 message hello python world print message message hello python ...

Python變數和簡單資料型別

學習 前言 python3 message hello python world print message message hello python world print message message hello world print message this is a string thi...