Sep. 18, 2012

Strings Built-In Methods

Strings Built-In Methods

In this post I want to show what you can do with the built-in methods for Strings. Let's first create a string with the value of "Hello World"

string = "Hello World"

To manipulate strings, we can use some of Pythons built-in methods

string.upper()   # get all-letters in uppercase

string.lower()    # get all-letters in lowercase

string.capitalize() # capitalize the first letter

string.title()    # capitalize the first letter of words

string.swapcase() # converts uppercase and lowercase

string.strip()    # remove all white spaces

string.lstrip()   # removes white space from left

string.rstrip()   # removes white space from right

string.split()    # splitting words

string.split(',') # split words by comma

string.count('l')   # count how many times l is in the string

string.find('Wo') # find the word Wo in the string

string.index("Wo")  # find the letters Wo in the string

":".join(string)  # add a : between every char

" ".join(string)  # add a white space between every char

len(string)   # find the length of the string

Recommended Python Training – DataCamp

For Python training, our top recommendation is DataCamp.

Datacamp provides online interactive courses that combine interactive coding challenges with videos from top instructors in the field.

Datacamp has beginner to advanced Python training that programmers of all levels benefit from.


Read more about:
Disclosure of Material Connection: Some of the links in the post above are “affiliate links.” This means if you click on the link and purchase the item, I will receive an affiliate commission. Regardless, only recommend products or services that we try personally and believe will add value to our readers.