Exploring String Methods
Strings are central to programming; almost every program uses strings in some way. We’ll explore some of the ways in which we can manipulate strings and, at the same time, firm up our understanding of methods.
The most commonly used string methods are listed in Table 7, Common String Methods. (You can find the complete list in Python’s online documentation, or type help(str) into the shell.)
|
Method |
Description |
|---|---|
|
str.capitalize() |
Returns a copy of the string with the first letter capitalized and the rest lowercase |
|
str.count(s) |
Returns the number of nonoverlapping occurrences of s in the string |
|
str.endswith(end) |
Returns True iff the string ends with the characters ... |
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access