First I have to receive a string from the user. The function would be capitalizing the introduced string object. It would make the words start with uppercased characters and all remaining characters have lower case. Here is what I did:
ssplit = s.split()
for z in s.split():
if ord(z[0]) < 65 or ord(z[0])>90:
l=(chr(ord(z[0])-32))
new = l + ssplit[1:]
print(new)
else:
print(s)
I can't see what I am doing wrong.
Using str.title()
as suggested by @Pyer is nice. If you need to use chr
and ord
you should get your variables right - see comments in code:
s = "this is a demo text"
ssplit = s.split()
# I dislike magic numbers, simply get them here:
small_a = ord("a") # 97
small_z = ord("z")
cap_a = ord("A") # 65
delta = small_a - cap_a
for z in ssplit : # use ssplit here - you created it explicitly
if small_a <= ord(z[0]) <= small_z:
l = chr(ord(z[0])-delta)
new = l + z[1:] # need z here - not ssplit[1:]
print(new)
else:
print(s)
Output:
This
Is
A
Demo
Text