Python Forum
Python capitalize() method problem - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Python capitalize() method problem (/thread-7844.html)



Python capitalize() method problem - Skipper - Jan-27-2018

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."
print(txt.capitalize())
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

Thanks!
(Spyder 3.6/Win8.1Pro/32bit)


RE: Python capitalize() method problem - j.crater - Jan-27-2018

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.


RE: Python capitalize() method problem - Skipper - Jan-27-2018

J.Crater,
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()
    [/list

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

    Thank you!



RE: Python capitalize() method problem - buran - Jan-27-2018

check str.title()


RE: Python capitalize() method problem - j.crater - Jan-27-2018

If you are not allowed to use split(), you can surely do string slicing in my 2nd suggestion ;)


RE: Python capitalize() method problem - Mekire - Jan-27-2018

I think it is kinda dumb, but the description of str.capitalize does explicitly say this:
Help on method_descriptor:

capitalize(...)
    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.'
>>>



RE: Python capitalize() method problem - buran - Jan-27-2018

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."


RE: Python capitalize() method problem - j.crater - Jan-27-2018

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 =)


RE: Python capitalize() method problem - Mekire - Jan-27-2018

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.


RE: Python capitalize() method problem - Skipper - Jan-27-2018

Thank you all, guys!

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

@Mekire
I'd tend to agree with your last post.