将字符串大写字母改为小写字母
时间: 2023-09-19 18:10:19 浏览: 69
可以使用Python的lower()方法将字符串中的大写字母转换为小写字母。例如:
```python
string = "HELLO WORLD"
lowercase_string = string.lower()
print(lowercase_string)
```
输出结果为:hello world
相关问题
汇编语言子程序将字符串中的小写字母改为大写字母
下面是一个汇编语言子程序,可以将一个字符串中的小写字母改为大写字母:
```
; 输入:
; SI - 指向待转换的字符串
; 输出:
; 无
convert_lowercase_to_uppercase:
mov cl, [si] ; 将字符串中的一个字符读入 cl
cmp cl, 0 ; 如果字符为结束符,则结束转换
je end_loop
cmp cl, 'a' ; 检查是否小于 'a'
jl not_lowercase
cmp cl, 'z' ; 检查是否大于 'z'
jg not_lowercase
sub cl, 32 ; 转换为大写字母
mov [si], cl ; 将转换后的字符写回到字符串中
not_lowercase:
inc si ; 指向下一个字符
jmp convert_lowercase_to_uppercase
end_loop:
ret
```
该子程序首先将待转换的字符串的地址放入 SI 寄存器中,然后使用一个循环,逐个读取字符串中的字符。对于每个字符,如果该字符为小写字母,则将其转换为大写字母,然后将转换后的字符写回到字符串中。最后使用 ret 指令返回。
使用该子程序时,需要将待转换的字符串的地址放入 SI 寄存器中,然后调用该子程序。调用结束后,字符串中的所有小写字母都将被转换为大写字母。
Python将字符串中每个小写字母改为大写,for循环
可以使用Python中的字符串方法`upper()`将每个小写字母改为大写字母,然后使用for循环遍历字符串中的每个字符并应用该方法。以下是代码示例:
```python
string = "This is a sample string."
for i in range(len(string)):
if string[i].islower():
string = string[:i] + string[i].upper() + string[i+1:]
print(string)
```
输出:
```
THIS IS A SAMPLE STRING.
```
在上面的代码中,我们首先定义了一个字符串`string`。然后使用for循环遍历字符串中的每个字符。如果字符是小写字母,则使用字符串方法`upper()`将其转换为大写字母,然后将其放回到字符串中。最后,打印输出已更改的字符串。