Reputation: 39
To code a name like DeAnna you type:
name = "de\aanna"
and
print(name.title())
In this code \a
capitalizes a normally uncapitalized letter. What do you code to produce a name like "George von Trapp"
where I want to uncapitalize a normally capitalized letter?
Upvotes: 3
Views: 472
Reputation: 51989
The \a
does not capitalize a letter - it is the bell escape sequence.
The str.title
simply capitalizes the first letter of any group of letters. Since the bell is not a letter, it has the same meaning as a space. The following produces equivalent capitalization:
name = "de anna"
print(name.title())
Anyways, there are no capitalize/uncapitalize magic characters in python. Simply write the name properly. If you want both a proper and a lower-case version, create the later via str.lower
:
name = "George von Trapp"
print(name, ':', name.lower())
If you really want to go from "georg van trapp"
(I'm just pretending the discussion about \a
is over) to "Georg van Trapp"
- welcome to having-to-decide-about-the-semantics-of-the-language-you-are-emulating.
A simple approach is to upper-case every word, but fix some known ones.
name = "georg van trapp"
proper_name = name.title()
proper_name.replace(' Von ', ' von ').replace(' Zu ', ' zu ').replace(' De ', ' de ')
print(name, ':', proper_name)
You can do that with a list
-and-loop approach for less headache as well:
lc_words = ['von', 'van', 'zu', 'von und zu', 'de', "d'", 'av', 'af', 'der', 'Teer', "'t", "'n", "'s"]
name = "georg van trapp"
proper_name = name.title()
for word in lc_words:
proper_name = proper_name.replace(' %s ' % word.title(), ' %s ' % word)
print(name, ':', proper_name)
If names are of the form First Second byword Last
, you can capitalize everything but the second-to-last word:
name = "georg fritz ferdinand hannibal van trapp"
proper_name = name.title().split() # gets you the *individual* words, capitalized
proper_name = ' '.join(proper_name[:-2] + [proper_name[-2].lower(), proper_name[-1]])
print(name, ':', proper_name)
Any words that are shorter than four letters (warning, not feasible for some names!!!)
name = "georg fritz theodores ferdinand markus hannibal von und zu trapp"
proper_name = ' '.join(word.title() if len(word) > 3 else word.lower() for word in name.split())
print(name, ':', proper_name)
Upvotes: 5
Reputation: 2378
Why not just roll your own function for it?
def capitalizeName(name):
#split the name on spaces
parts = name.split(" ")
# define a list of words to not capitalize
do_not_cap = ['von']
# for each part of the name,
# force the word to lowercase
# then check if it is a word in our forbidden list
# if it is not, then go ahead and capitalize it
# this will leave words in that list in their uncapitalized state
for i,p in enumerate(parts):
parts[i] = p.lower()
if p.lower() not in do_not_cap:
parts[i] = p.title()
# rejoin the parts of the word
return " ".join(parts)
The point to the do_not_cap
list is that allows you to further define parts you may not want to capitalize very easily. For example, some names may have a "de" in it you may not want capitalized.
This is what it looks like with an example:
name = "geOrge Von Trapp"
capitalizeName(name)
# "George von Trapp"
Upvotes: 1
Reputation: 16127
What do you code to produce a name like
"George von Trapp"
where I want to uncapitalize a normally capitalized letter?
Letters are not auto-capitalized in Python. In your case, "de\aanna"
(I think you should use "de anna"
instead) is capitalised because you called title()
on it. If I didn't misunderstand your question, what you want is simply to disable such "auto-capitalizing".
Just don't call title()
:
name = "George von Trapp"
print(name.lower())
Upvotes: -1