Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使单词的第一个字符大写,如果单词后跟数字,那么如何使第一个字母大写?_Python_Python 3.x_List - Fatal编程技术网

Python 如何使单词的第一个字符大写,如果单词后跟数字,那么如何使第一个字母大写?

Python 如何使单词的第一个字符大写,如果单词后跟数字,那么如何使第一个字母大写?,python,python-3.x,list,Python,Python 3.x,List,假设输入字符串是1w2r3g,那么输出应该是1w2r3g。 代码如下: s='hello kiran 3g' s=s.title().splitlines() print(s) for ele in s: if ele[0].isalpha: ele[0].upper() else: ele[1].upper() s1=' '.join(s) print(s1) 使用带有lookback-->的正则表达式(?hello kiran 3G 打印(r

假设输入字符串是
1w2r3g
,那么输出应该是
1w2r3g
。 代码如下:

s='hello kiran 3g'
s=s.title().splitlines()
print(s)
for ele in s:
    if ele[0].isalpha:
        ele[0].upper()
    else:
        ele[1].upper()
s1=' '.join(s)
print(s1)

使用带有
lookback
-->
的正则表达式(?hello kiran 3G

打印(re.sub)(r)(?您可以这样做

import re
s='hello kiran 3guru'
ss=s.split()


for i,j in enumerate(ss):
    if not j.isalpha():
        x=re.findall(r'\d+',j)
        if j.startswith(str(x[0])):
            ss[i]=j[:len(x[0])]+j[len(x[0]):].title()

print(' '.join(ss))

s1='hello34 kiran 3guru'
ss=s1.split()

for i,j in enumerate(ss):
    if not j.isalpha():
        x=re.findall(r'\d+',j)
        if j.startswith(str(x[0])):
            ss[i]=j[:len(x[0])]+j[len(x[0]):].title()

print(' '.join(ss))

s2='hello24 kir24an 34guru45'
ss=s2.split()
for i,j in enumerate(ss):
    if not j.isalpha():
        x=re.findall(r'\d+',j)
        if j.startswith(str(x[0])):
            ss[i]=j[:len(x[0])]+j[len(x[0]):].title()


print(' '.join(ss))


isalpha()
将返回
True
如果整个字符串只包含字母,否则
False
s.split(“”)
将返回一个列表
['hello',kiran','3g']
。然后使用
upper()
转换
'3g'
,并使用
join()

你能把代码放在代码块中吗?这样更容易阅读一些解释会更好-这种“仅代码”的回答倾向于鼓励“复制粘贴编程”",我们都知道这是一场灾难。@Rakesh假设
s='hel34lo kiran 3guru'
那么根据您的代码输出的是
hel34lo kiran 3guru
。但是OP希望输出是
hel34lo kiran 3guru
我只想制作第一个字母大写字母,即使这个单词包含第一个数字字符,那么,在这种情况下,它就是应该只使用第一个字符大写,而不是所有字符大写。@surenderpal我已经更新了我的答案,请检查。@surenderpal我已经使用了
re.findall
@surenderpal如果我的答案旁边的勾号有助于整个社区识别正确答案,请接受答案。新年快乐。
import re
s='hello kiran 3guru'
ss=s.split()


for i,j in enumerate(ss):
    if not j.isalpha():
        x=re.findall(r'\d+',j)
        if j.startswith(str(x[0])):
            ss[i]=j[:len(x[0])]+j[len(x[0]):].title()

print(' '.join(ss))

s1='hello34 kiran 3guru'
ss=s1.split()

for i,j in enumerate(ss):
    if not j.isalpha():
        x=re.findall(r'\d+',j)
        if j.startswith(str(x[0])):
            ss[i]=j[:len(x[0])]+j[len(x[0]):].title()

print(' '.join(ss))

s2='hello24 kir24an 34guru45'
ss=s2.split()
for i,j in enumerate(ss):
    if not j.isalpha():
        x=re.findall(r'\d+',j)
        if j.startswith(str(x[0])):
            ss[i]=j[:len(x[0])]+j[len(x[0]):].title()


print(' '.join(ss))
hello kiran 3Guru
hello24 kiran 3Guru
hello24 kir24an 34Guru45