How to capitalize first letter in strings that may contain numbers
Using regular expressions:
for line in output:
m = re.search('[a-zA-Z]', line);
if m is not None:
index = m.start()
output.write(line[0:index] + line[index].upper() + line[index + 1:])
You can write a function with a for
loop:
x = "hello world"
y = "11hello world"
z = "66645world hello"
def capper(mystr):
for idx, i in enumerate(mystr):
if not i.isdigit(): # or if i.isalpha()
return ''.join(mystr[:idx] + mystr[idx:].capitalize())
return mystr
print(list(map(capper, (x, y, z))))
['Hello world', '11Hello world', '66645World hello']
You can use regular expression to find the position of the first alphabet and then use upper()
on that index to capitalize that character. Something like this should work:
import re
s = "66645hello world"
m = re.search(r'[a-zA-Z]', s)
index = m.start()