Guibs 的 Python學習_字串
# 字串# Python 中, 用引號括起來的都是字串. 引號可以是單引號, 也可以是雙引號"This is a string"'This is also a string'# 因而可以在字串中包含 " 或 ''I told myself, "you are Gubis"'"The name 'Guibs' is my nickname"# 首字母大寫指定字串# [.title()]name = 'Macbook pro'print(name.title())# 全部大寫指定字串# [.upper()]print(name.upper())# 全部小寫字串# [.lower()]print(name.lower())# 合并(拼接)字串# [使用 + 拼接字串]first_name = "guibs"last_name = 'g'full_name = first_name + " " + last_nameprint(full_name)hello_message = "Hello, " + full_name.title() + "!"print(hello_message)# 定位字元# [\t]print("Python")print("\tPython")# 分行符號# [\n]print("123")print("1\n2\n3")# 刪除字串空白# [.rstrip() 刪除字串末尾多餘空白]print("刪除字串 Swift and Python 末尾多餘空白")favorite_language = " Swift and Python "print(favorite_language + " are my favorite languages")print(favorite_language.rstrip() + " are my favorite languages" + "\n")# [.lstrip() 刪除字串開頭多餘空白]print("刪除字串 Swift and Python 開頭多餘空白")print(favorite_language + " are my favorite languages")print(favorite_language.lstrip() + " are my favorite languages" + "\n")# [.strip() 刪除字串頭尾多餘空白]print("刪除字串 Swift and Python 頭尾多餘空白")print(favorite_language + " are my favorite languages")print(favorite_language.strip() + " are my favorite languages" + "\n")# 正確使用引號# 保留 "" 顯示print('I told myself, "you are Gubis"')# 保留 '' 顯示print("The name 'Guibs' is my nickname")
以上就是Guibs 的 Python學習_字串的內容,更多相關內容請關注topic.alibabacloud.com(www.php.cn)!