當前位置:首頁 » 編程語言 » python字元串大小寫轉換

python字元串大小寫轉換

發布時間: 2025-06-02 20:39:53

1. python字元串大小寫轉換(3種)

在Python編程語言中,處理字元串大小寫轉換的任務可以通過三種內置函數輕松實現:title()、lower()和upper()。下面將分別介紹這三種函數的使用方法和效果。
1. title()函數:該函數將字元串中的每個單詞的首字母轉換為大寫,而其他字母則轉換為小寫。如果字元串中沒有單詞需要轉換,它將原樣返回。使用格式為:`str = str.title()`。例如:
```python
str = "hello world"
str = str.title()
# 轉換後: str = "Hello World"
```
2. lower()函數:這個函數將字元串中的所有大寫字母轉換為小寫,而原始小寫字母保持不變。用法示例:`str = str.lower()`。例如:
```python
str = "HELLO WORLD"
str = str.lower()
# 轉換後: str = "hello world"
```
3. upper()函數:與lower()函數相反,upper()函數將字元串中的所有小寫字母轉換為大寫,原始大寫字母保持不變。調用格式為:`str = str.upper()`。例如:
```python
str = "hello world"
str = str.upper()
# 轉換後: str = "HELLO WORLD"
```
需要注意的是,這三種函數都不會修改原始字元串,而是返回一個新的轉換後的副本。

熱點內容
java返回this 發布:2025-10-20 08:28:16 瀏覽:593
製作腳本網站 發布:2025-10-20 08:17:34 瀏覽:888
python中的init方法 發布:2025-10-20 08:17:33 瀏覽:582
圖案密碼什麼意思 發布:2025-10-20 08:16:56 瀏覽:765
怎麼清理微信視頻緩存 發布:2025-10-20 08:12:37 瀏覽:684
c語言編譯器怎麼看執行過程 發布:2025-10-20 08:00:32 瀏覽:1013
郵箱如何填寫發信伺服器 發布:2025-10-20 07:45:27 瀏覽:255
shell腳本入門案例 發布:2025-10-20 07:44:45 瀏覽:114
怎麼上傳照片瀏覽上傳 發布:2025-10-20 07:44:03 瀏覽:806
python股票數據獲取 發布:2025-10-20 07:39:44 瀏覽:713