How To Capitalize All Words in a String in Python – The Easy way

I run into this medium post today – “How To Capitalize All Words in a String in Python“.

It explains how to convert “hello world” to “Hello World” but it does it the hard way. An easier solution would be to use title function.

x = "hello world"
print(x.title())
#Hello World

See more documentation here

2 thoughts on “How To Capitalize All Words in a String in Python – The Easy way

  1. “Doesn’t behave as you’d hope”
    becomes
    “Doesn’T Behave As You’D Hope”

    The version he gives does work although it assumes that you don’t mistype and leave no space after a period.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s