3
sentence = str ( input ( "Enter a sentence:" ) )
sentence = sentence.split ( )

new = ""
for word in sentence:
    wordi = ord ( word[ 0 ] )
    cap = word[ 0 ]
    a = chr ( (ord ( cap ) - 32) )
    word1 = word.replace ( word[ 0 ] ,a )

    if wordi <= 122 and wordi >= 97:
        new = new + word1 + " "
    else:
        new = new + word + " "

print ( new )

I have been writing about a code that would capitalize all the first letter in the sentence without using the capitalize or upper function. The code that I wrote did appear to be alright when the letter in the word is not the same as the letter that I want to capitalize.

The input:

Hello world

The output:

Hello World

However, if the letter in the word is also the same as the letter that I want to capitalize, the letter within the word will also become capitalized.

The input:

helloh worldw

The output:

HelloH WorldW

I tried to switch the "a" variable inside the replacement and add a to new as well in the variable new in the if-else statement.

sentence = str ( input ( "Enter a sentence:" ) )
sentence = sentence.split ( )

new = ""
for word in sentence:
    wordi = ord ( word[ 0 ] )
    cap = word[ 0 ]
    a = chr ( (ord ( cap ) - 32) )
    word1 = word.replace ( word[ 0 ] ,"" )

    if wordi <= 122 and wordi >= 97:
        new = new + a + word1 + " "
    else:
        new = new + word + " "

print ( new )

But, the code turned out to be that the letter that is being repeated in the word will be deleted when printed.

The input:

helloh 

The output:

Hello

How will I be able to make the code work?

Peter
  • 57
  • 4

3 Answers3

2

You can use count parameter of the str.replace method, to replace once only

word1 = word.replace(word[0], a, 1)

Put all in a method, to facilitate use, and you got

def capitalize(sentence):
    if isinstance(sentence, str):    # handle string, if wasn't splitted already
        sentence = sentence.split()
    new = ""
    for word in sentence:
        ow = ord(word[0])
        if 97 <= ow <= 122:
            new += word.replace(word[0], chr(ow - 32), 1) + " "
        else:
            new += word + " "
    return new


print(capitalize(["Hello", "worldw"]))  # Hello Worldw
print(capitalize("Hello worldw"))       # Hello Worldw
azro
  • 53,056
  • 7
  • 34
  • 70
2
def capitalize(lower_case_word):
    return ' '.join(x[:1].upper() + x[1:] for x in lower_case_word.split())

print(capitalize('hello I am nobody'))

To capitalize all the first characters, this is the one with the best efficiency (by lines).

def capitalize(lower_case_word):
    lower_case_word = lower_case_word.split()
    new_phrase = ""

    for character in lower_case_word:
        new_phrase += character[0].upper() + character[1:] + ' '

    return new_phrase

print(capitalize('hello I am nobody'))

This one also works, but it takes several more lines to finish coding. Personally, I recommend the second method if you are a beginner, it is a lot easier to understand since I am also one.

Dolfinwu
  • 262
  • 1
  • 14
1

Since you can't use the str.upper() method, let's define an upper() function ourselves:

def upper(char):
    # If more than one characters are given, capitalize all of them 
    if len(char) > 1:
        return ''.join(upper(c) for c in char)
    
    # if given character is between lowercase a and z, shift it to uppercase
    if "a" <= char <= "z":
        return chr(ord(char) - 32)
    else: # if not, return it unchanged
        return char

You want to replace only the first character with the capitalized one. Unfortunately, strings are immutable so you can't simply do word[0] = word[0].upper()

However, you can rebuild the string by slicing its first character from the rest like so: word = word[0].upper() + word[1:].

Or, using our upper() function: word = upper(word[0]) + word[1:].

Implementing this with your code:

sentence = str ( input ( "Enter a sentence:" ) )
sentence = sentence.split ( )

newsentence = []
for word in sentence:
    newword = upper(word[0]) + word[1:]
    newsentence.append(newword)

print(' '.join(newsentence))

Of course, the for-loop can be replaced by a list comprehension,

sentence = str ( input ( "Enter a sentence:" ) )
sentence = sentence.split ( )

newsentence = [upper(word[0]) + word[1:] for word in sentence]
print(' '.join(newsentence))

# Or you could pass the generator expression to the `str.join()` call
# To do this, replace the previous two lines with:
# print(' '.join(word[0].upper() + word[1:] for word in sentence))

Running this with your input helloh worldw gives the output you expect:

Enter a sentence:helloh worldw
Helloh Worldw
Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70