7 examples of 'python title case' in Python

Every line of 'python title case' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
18def titlecase(s):
19 """Format string in title case.
20
21 Only changes the first character of each word.
22 """
23 return re.sub(r"[A-Za-z]+('[A-Za-z]+)?",
24 lambda m: m.group(0)[0].upper() + m.group(0)[1:], s)
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

25def titlecase(text):
26 """
27 Similar to string.title() but with some improvements.
28 """
29 return re.sub(r"[A-Za-z]+('[A-Za-z]+)?",
30 lambda mo: mo.group(0)[0].upper() +
31 mo.group(0)[1:].lower(),
32 text)
92def titleize(s):
93 rv=s[0].upper() + s[1:]
94 return rv
32def underscore(title):
33 return re.sub(r"(?<=[a-z])(?=[A-Z])", u"_", title).lower()
10def title_exceptions(word, **kwargs):
11
12 word_test = word.strip("(){}<>.")
13
14 # lowercase words
15 if word_test.lower() in ['a', 'an', 'of', 'the', 'is', 'or']:
16 return word.lower()
17
18 # uppercase words
19 if word_test.upper() in ['UK', 'FM', 'YMCA', 'PTA', 'PTFA',
20 'NHS', 'CIO', 'U3A', 'RAF', 'PFA', 'ADHD',
21 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X', 'XI',
22 'AFC', 'CE', 'CIC'
23 ]:
24 return word.upper()
25
26 # words with no vowels that aren't all uppercase
27 if word_test.lower() in ['st', 'mr', 'mrs', 'ms', 'ltd', 'dr', 'cwm', 'clwb', 'drs']:
28 return None
29
30 # words with number ordinals
31 if bool(ORD_NUMBERS_RE.search(word_test.lower())):
32 return word.lower()
33
34 # words with dots/etc in the middle
35 for s in [".", "'", ")"]:
36 dots = word.split(s)
37 if len(dots) > 1:
38 # check for possesive apostrophes
39 if s == "'" and dots[-1].upper() == "S":
40 return s.join([titlecase.titlecase(i, title_exceptions) for i in dots[:-1]] + [dots[-1].lower()])
41 # check for you're and other contractions
42 if word_test.upper() in ["YOU'RE", "DON'T", "HAVEN'T"]:
43 return s.join([titlecase.titlecase(i, title_exceptions) for i in dots[:-1]] + [dots[-1].lower()])
44 return s.join([titlecase.titlecase(i, title_exceptions) for i in dots])
45
46 # words with no vowels in (treat as acronyms)
47 if not bool(VOWELS.search(word_test)):
48 return word.upper()
49
50 return None
73def to_title(s):
74 return s.title()
57def title_case(string, seperator='_'):
58 """Format a string into title case - e.g. 'some_word' => Some Word """
59 return ' '.join(map(firstcaps, string.split(seperator)))

Related snippets