Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Python capitalize() method problem
Hi Guys,

I am new to Python and programming and just started a Python tutorial - currently about string methods.
The drill is the following:
Print the string variable capitalizing only the first letter, to turn "the TIME is Noon." into "The TIME is Noon."

What I did is this:
txt = "the TIME is Noon."
What I get is this:
=> The time is noon. - what's OK according to Python capitalize() definition
What I need is this:
=> "The TIME is Noon"

The thing is that I have to follow the tutorial curriculum and for now - at its beginning - I am limited to string methods, concatenation, printing and should not reach beyond.
Searched the Net for solutions - found nothing.
Any suggestions as to any other methods (or solutions within the above framework) most welcome.

May be as simple as 1,2,3... but I am stuck Huh

(Spyder 3.6/Win8.1Pro/32bit)
Hello and welcome to Python and the forums!

You can split the string, use capitalize() only on first word, and then put strings back together.

You can also use string's upper() method. It changes all characters in string to upper case. So you can slice the string and change only first character to upper, and leave the rest as they are.
thank you very much for your welcoming words and such a speedy feedback.

In my initial post I made the mistake of not specifying the methods "auhorized" as for now in the tutorial.
These are:
  • • .isalpha()
    • .isalnum()
    • .istitle()
    • .isdigit()
    • .islower()
    • .isupper()
    • .startswith()
    • .capitalize()
    • .lower()
    • .upper()
    • .swapcase()
    • .title()

    I tend to think the drill itself is unrelated to current tutorial stage, and I'll use split().

    Thank you!
check str.title()
If you are not allowed to use split(), you can surely do string slicing in my 2nd suggestion ;)
I think it is kinda dumb, but the description of str.capitalize does explicitly say this:
Help on method_descriptor:

    S.capitalize() -> str

    Return a capitalized version of S, i.e. make the first character
    have upper case and the rest lower case.

This means it is not the right tool (neither is str.title as it also changes the rest of the string).
>>> txt[0].upper() + txt[1:]
'The TIME is Noon.'
by the way the assignment description and the example contradict to some extent
Print the string variable capitalizing only the first letter, to turn "the TIME is Noon." into "The TIME is Noon."
I think the exercise wants students to find a way to capitalize the first letter of string, without lowercasing "TIME" and uppercasing "is". It's quite neatly designed I think =)
Depends if the goal is to only familiarize them with string methods, or slightly more.

As I see it, it can't be done with just string methods without using a little indexing and slicing at least.
buran likes this post
Thank you all, guys!

1. slice() is unauthorized Undecided but I'll use it anyway.
2. your interpretation of the exercise is perfectly correct

I'd tend to agree with your last post.

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Find and replace to capitalize with Regex hermobot 2 364 Mar-21-2020, 12:30 PM
Last Post: hermobot
  Problem with adding arbitrary parrameters to __init__ method sebastianvdn 1 269 Feb-03-2020, 09:30 PM
Last Post: micseydel
  [split] capitalize dict keys for display in string newbieAuggie2019 3 549 Oct-10-2019, 10:50 AM
Last Post: perfringo
  problem with class method AmirAB 3 729 Feb-13-2019, 01:51 AM
Last Post: AmirAB

Forum Jump:

Users browsing this thread: 1 Guest(s)