Python Forum
Python capitalize() method problem
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)
Reply
#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.
Reply
#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!
Reply
#4
check str.title()
Reply
#5
If you are not allowed to use split(), you can surely do string slicing in my 2nd suggestion ;)
Reply
#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.'
>>>
Reply
#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."
Reply
#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 =)
Reply
#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.
Reply
#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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem usage of static method akbarza 5 455 Feb-03-2024, 07:43 AM
Last Post: paul18fr
  How to capitalize in dictionary Inkanus 2 3,618 Oct-28-2020, 01:20 PM
Last Post: Inkanus
  Find and replace to capitalize with Regex hermobot 2 2,483 Mar-21-2020, 12:30 PM
Last Post: hermobot
  Problem with adding arbitrary parrameters to __init__ method sebastianvdn 1 1,943 Feb-03-2020, 09:30 PM
Last Post: micseydel
  [split] capitalize dict keys for display in string newbieAuggie2019 3 2,937 Oct-10-2019, 10:50 AM
Last Post: perfringo
  problem with class method AmirAB 3 3,332 Feb-13-2019, 01:51 AM
Last Post: AmirAB

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020