Trojosh
Trojosh

Reputation: 565

Python How to capitalize nth letter of a string

I tried this: Capitalize a string. Can anybody provide a simple script/snippet for guideline?

Python documentation has capitalize() function which makes first letter capital. I want something like make_nth_letter_cap(str, n).

Upvotes: 15

Views: 51922

Answers (9)

Ajay. S
Ajay. S

Reputation: 1

To capitalise the nth letter in the given string

def nth_letter_uppercase(string,n):

  listofwords = string.split()
  sentence_upper = ''

  for word in listofwords:

    length = len(word)
    

    if length > (n - 1):
      new_word = word[:n-1] + word[n-1].upper() + word[n:]
     

    else:
      new_word = word

    sentence_upper += ' ' + new_word
    H=sentence_upper[n-1:]

  return H

String=str(input("Enter String :"))

nth_letter_uppercase(String,2)  #n is named as 2 here you can use any value you want to change

Then in console screen

Enter String :hi here is your solution
hI hEre iS yUor sOlution

I had just polished the code by deleting the space infront of the string

Upvotes: 0

gokulrejithkumar
gokulrejithkumar

Reputation: 11

def capitalize_n(string, n):
    return string[:n] + string[n].capitalize() + string[n+1:]

This works perfect

Upvotes: 0

Sagar Deb
Sagar Deb

Reputation: 11

This is the comprehensive solution: either you input a single word, a single line sentence or a multi line sentence, the nth letter will be converted to Capital letter and you will get back the converted string as output:

You can use this code:

def nth_letter_uppercase(string,n):
  
  listofwords = string.split()
  sentence_upper = ''

  for word in listofwords:
  
    length = len(word)
      
    if length > (n - 1):
      new_word = word[:n-1] + word[n-1].upper() + word[n:]
      
    else:
      new_word = word
          
    sentence_upper += ' ' + new_word

  return sentence_upper

calling the function defined above (I want to convert 2nd letter of each word to a capital letter):

string = '''nature is beautiful
and i love python'''
nth_letter_uppercase(string,2)

output will be:

'nAture iS bEautiful aNd i lOve pYthon'

Upvotes: 1

Shounak Kulkarni
Shounak Kulkarni

Reputation: 36

You can use:

def capitalize_nth(text, pos):
    before_nth = text[:pos]
    n = text[pos].upper()
    new_pos = pos+1
    after_nth = text[new_pos:]
    word = before_nth + n + after_nth
    print(word)

capitalize_nth('McDonalds', 6)

The outcome is:

'McDonaLds'

I think this is the simplest among every answer up there...

Upvotes: 0

roopali k
roopali k

Reputation: 1

A simplified answer would be:

    def make_nth_letter_capital(word, n):
        return word[:n].capitalize() + word[n:].capitalize()

Upvotes: 0

ZiP
ZiP

Reputation: 91

I know it's an old topic but this might be useful to someone in the future:

def myfunc(str, nth):
new_str = '' #empty string to hold new modified string
for i,l in enumerate(str): # enumerate returns both, index numbers and objects
    if i % nth == 0: # if index number % nth == 0 (even number)
        new_str += l.upper() # add an upper cased letter to the new_str
    else: # if index number nth
        new_str += l # add the other letters to new_str as they are
return new_str # returns the string new_str

Upvotes: 0

cppcoder
cppcoder

Reputation: 23105

x = "string"
y = x[:3] + x[3].swapcase() + x[4:]  

Output

strIng  

Code

Keep in mind that swapcase will invert the case whether it is lower or upper.
I used this just to show an alternate way.

Upvotes: 2

jfs
jfs

Reputation: 414205

Capitalize n-th character and lowercase the rest as capitalize() does:

def capitalize_nth(s, n):
    return s[:n].lower() + s[n:].capitalize()

Upvotes: 22

icktoofay
icktoofay

Reputation: 129011

my_string[:n] + my_string[n].upper() + my_string[n + 1:]

Or a more efficient version that isn't a Schlemiel the Painter's algorithm:

''.join([my_string[:n], my_string[n].upper(), my_string[n + 1:]])

Upvotes: 14

Related Questions