python 将单词的大写转换成小写
时间: 2023-06-26 16:09:36 浏览: 40
在 Python 中,可以使用 `lower()` 方法将字符串中的大写字母转换成小写字母。例如:
```python
word = "HELLO"
lowercase_word = word.lower()
print(lowercase_word) # 输出 "hello"
```
如果你想将整个字符串中的所有单词的大写字母转换成小写字母,可以使用 `split()` 方法将字符串分割成单词,然后使用 `lower()` 方法将每个单词的大写字母转换成小写字母,最后使用 `join()` 方法将单词重新组合成字符串。例如:
```python
sentence = "I LOVE PYTHON"
words = sentence.split() # 将字符串分割成单词
lowercase_words = [word.lower() for word in words] # 将单词中的大写字母转换成小写字母
lowercase_sentence = " ".join(lowercase_words) # 将单词重新组合成字符串
print(lowercase_sentence) # 输出 "i love python"
```
相关问题
python中大小写转换函数
在 Python 中,我们可以使用以下字符串函数来进行大小写转换:
1. `str.upper()`:将字符串中的所有字符转换为大写字母。
2. `str.lower()`:将字符串中的所有字符转换为小写字母。
3. `str.capitalize()`:将字符串的第一个字符转换为大写字母,其他字符转换为小写字母。
4. `str.title()`:将字符串中的每个单词的第一个字母转换为大写字母,其他字母转换为小写字母。
下面是一些示例代码:
```python
# 将字符串转换为大写字母
string = "Hello, World!"
print(string.upper())
# 将字符串转换为小写字母
string = "Hello, World!"
print(string.lower())
# 将字符串的第一个字符转换为大写字母,其他字符转换为小写字母
string = "hello, world!"
print(string.capitalize())
# 将字符串中的每个单词的第一个字母转换为大写字母,其他字母转换为小写字母
string = "hello, world!"
print(string.title())
```
python大小写字母转换
以下是Python中大小写字母转换的三种方法:
1.使用lower()函数将字符串中的所有字符转换为小写:
```python
s = "Hello, Python!"
print(s.lower()) # 输出:"hello, python!"
```
2.使用upper()函数将字符串中的所有字符转换为大写:
```python
s = "Hello, Python!"
print(s.upper()) # 输出:"HELLO, PYTHON!"
```
3.使用title()函数将字符串中的每个单词的首字母转换为大写,其余部分转换为小写:
```python
s = "hello, python!"
print(s.title()) # 输出:"Hello, Python!"
```
另外,如果想去除字符串两端的空白字符,可以使用strip()函数:
```python
s = " Hello, Python! "
print(s.strip()) # 输出:"Hello, Python!"
```