There are a host of methods available for processing string objects. Here are a few examples. At this point it is sensible to introduce the comment character, #. The comment character allows you to put comments into your code for the purpose of documentation. While comments are not considered executable code, they are extremely useful for making your code readable and understandable.
#explore changing to uppercase and lowercase a='good' c=a.upper() d=c.lower() print(c) print(d) #join a list of strings together with a space in between the strings b='morning' e=' '.join([a,b,'today']) print(e) #find a string within a string #find method returns the first index where string was found x='a picture is worth a thousand words' x1=x.find('picture') print(x1) x2=x.find('worth') print(x2) x3=x.find('words') print(x3) #split up a string into a list of smaller strings #use the ' ' space character as the boundary (delimiter) to split up the string y=x.split(' ') print(y) print(type(y)) #try the replace method ... z=x.replace('thousand', 'million') print(x) print(z)
Take some time to explore your own versions of these examples.
String Methods
Here are some of the most common string methods. A method is like a function, but it runs "on" an object. If the variable s is a string, then the code s.lower() runs the lower() method on that string object and returns the result (this idea of a method running on an object is one of the basic ideas that make up Object Oriented Programming, OOP). Here are some of the most common string methods:Source: Google for Education, https://developers.google.com/edu/python/strings#string-methods
This work is licensed under a Creative Commons Attribution 4.0 License.