Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Python capitalize() method problem
#1
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)
Quote
#2
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.
Quote
#3
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!
Quote
#4
check str.title()
Quote
#5
If you are not allowed to use split(), you can surely do string slicing in my 2nd suggestion ;)
Quote
#6
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.'
>>>
Quote
#7
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."
Quote
#8
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 =)
Quote
#9
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
Quote
#10
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.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  [split] capitalize dict keys for display in string newbieAuggie2019 3 239 Oct-10-2019, 10:50 AM
Last Post: perfringo
  problem with class method AmirAB 3 400 Feb-13-2019, 01:51 AM
Last Post: AmirAB

Forum Jump:


Users browsing this thread: 1 Guest(s)