小编Din*_*ino的帖子

如何将可能包含数字的字符串中的第一个字母大写

我想通读一个文件并使用Python将字符串中的第一个字母大写,但有些字符串可能首先包含数字.具体来说,该文件可能如下所示:

"hello world"
"11hello world"
"66645world hello"
Run Code Online (Sandbox Code Playgroud)

我希望这是:

"Hello world"
"11Hello world"
"66645World hello"
Run Code Online (Sandbox Code Playgroud)

我已尝试过以下内容,但只有在字母位于第一位置时才会大写.

with open('input.txt') as input, open("output.txt", "a") as output:
    for line in input:
        output.write(line[0:1].upper()+line[1:-1].lower()+"\n")
Run Code Online (Sandbox Code Playgroud)

有什么建议?:-)

python string capitalization

9
推荐指数
1
解决办法
324
查看次数

标签 统计

capitalization ×1

python ×1

string ×1