DEV Community

Discussion on: Challenge-- Capitalize each word in a string

Collapse
 
nimonian profile image
Simon Haines

I was playing around with this a few ways in Python. I wondered if just passing through the characters once would be better than splitting and joining which passes through the words multiple times. First up, make a big string.

from time import time
a = 'hello this is my string it is lovely!'*10000
a = a.strip()

First try:

t = time()
A = a[0].upper()
for i in range(1,len(a)):
    c = a[i]
    A += c.upper() if (a[i-1] == ' ') else c
print(time() - t)

This takes 5.04s (3 s.f.). Maybe looking up the value of the string at a[i-1] is too slow, so here's another try using flags to speed things up:

t = time()
A = ''
flag = True
for c in a:
    if flag == True:
        c = c.upper()
        flag = False
    A += c
    if c == ' ':
        flag = True
print(time() - t)

The time becomes 4.44s (3 s.f.), so we've shaved a bit off. Then I tried the Python equivalent of @jasterix solution:

t = time()
A = ' '.join(w[0].upper() + w[1:] for w in a.split(' '))
print(time() - t)

The time... 0.0615s (3 s.f.). Now, although the first two pass through each character whereas the last one only passes through the words, surely split(' ') needs to examine each character to decide if it is ' ' or not? This got me thinking, so I tried:

A = ''
t = time()
for c in a:
    A += c
print(time() - t)

Just going through the string and adding each character to the end of an new string takes 4.20s (3 s.f.) seconds! Compared with

t = time()
A = ''.join(c for c in a)
print(time() - t)

which takes 0.0246s.

In short, I conclude that split() and join() are magic and I will use them for building strings over += in Python from now on, where possible!