Tutorials Python Primitive Data Types String functions 04. Strings methods
Strings have various methods that are used for logical testing. Briefly, let's go over the most commonly used methods, with two strings
Check if string consists of all letters.
s with first letter and lowercase rest.
Check if string
s consists of all alphanumeric characters.
True if not empty and all characters are digits.
True if not empty and all numeric.
True if not empty and its characters can be used in forming decimal-radix numbers including unicode number values and names.
Return a copy of string with upper converted to lowercase and vice versa.
True if contains at least one cased character and all of its cased characters are lowercase.
True if all cased characters are uppercase and there is at least one cased character.
Not empty and all its uppercase characters follow uncased characters and all its lowercase characters follow cased characters.
Return total occurrences of character i.
Return numeric string filled with zeros for a length of w. Searching
It's easy to search for a substring. Assume that
i is the start position and
j is the end position.
s.startswith(t[, i, [j]])
True if s starts with t.
s.endswith(t[, i, [j]])
True if s ends with t.
s.find(t[, i[, j]])
Return the lowest index of s at which t is found. Return -1 if not found.
s.rfind(t[, i[, j]])
Return highest index where t is found.
s.index(t[, i[, j]])
find() but ValueError instead of -1 if not found.
s.rindex(t[, i[, j]])
rfind() but ValueError instead of -1 if not found.
s.count(t[, i[, j]])
returns the number of occurrences of t in s. Replacing
Searching and replacing is also possible in Python through a handful of functions.
Replace all occurrences of t in s with u.
Translate string s with a dictionary. Changing case
Return lowercase form of s.
Return capitalized form of s.
Return string with each word beginning with an uppercase character and the rest lowercase.
Return string with lowercase letters made uppercase and vice versa. Parsing
Here are some functions that can help you get rid of any leading or trailing characters.
Return s with leading characters c removed.
Same as above but with trailing characters removed.
Returns s with leading and trailing character c removed. Learn to be a Pythonista! Learn Python in One Day
Have you always wanted to learn computer programming but are afraid it'll be too difficult for you? Or you're familiar with some programming but are interested in learning Python fast? Then this book is for you. You no longer have to waste your time and money learning Python from lengthy books, expensive online courses or complicated Python tutorials.
$ Check price (185+ reviews) More Python resources
Thank you for reporting bugs to firstname.lastname@example.org.
Disclaimer: Snipcademy is supported by your purchases of our recommended products through Amazon Services LLC and the ads posted on our website. All products listed are thoroughly researched, curated and reviewed so that viewers are given the best recommendations. Thank you and happy browsing!
Med - Code - Bioinformatics - Golf - Cat - Money - Astro -