- The string method lower will return the string with all characters changed to lowercase.The inverse of this is often the upper method,which will return the string beat uppercase.Just like with previous methods, we call these on a string using dot notation, like “this is string”. Upper ().
- This would return the string “This could also be a string”. this will be super handy when checking user input,since someone might type altogether lowercase,or even a mix of cases.
- We can use the strip method to get rid of surrounding white space from a string. White space includes spaces,tabs, and newline characters.
- We can also use the methods Istrip and rstrip to get rid of white space only from the left or the proper side of the string,respectively.
- The method count are often wont to return the amount of times as sub string appears in as string.This can be handy for locating out what percentage characters appear during a string, or counting the amount of times a particular word appears during a sentence or paragraph.
- If we wanted to see if a string ends with a given sub string,you can use the tactic enswith. This will return True if the substring is found at the top of the string, and False if not.
- This isnumeric method can check if a string consists of only numbers. If the string contains numbers before passing the string to the int() function to convert it to an integer, avoiding a mistake .Useful!
- We took a glance at string concatenation using the sign ,earlier.We can also use the join method to concatenate strings.This method takes an inventory of strings to be joined as a parameter, and returns a replacement string composed of every of the strings from our list joined using the initial string.For example,”“.join([This”,”is”,”a”,”sentence”]) would return the string “This may be a sentence”.
- The inverse of the join method is that the split method.This allows us to separate a string into an inventory of strings.By default,it splits by any white space characters.You can also split by any other characters by passing a parameter.