Learn how Python allow us to Manipulate Strings
In many cases, text is one of the most common forms of data in a program. All programming languages have to handle strings in one way or another. Python makes it easy to handle data with strings. You can concatenate two strings using + operator, convert a letter uppercase or lowercase, remove spacing in string, check if string contains an alphabet character and more.
Examples string literals
“That’s a nice car.”
“Today is Jane’s birthday.”
We can assign any of string literals above to a variable and print them on an interactive shell.
We can also change a string to lower or uppercase using some of the useful methods such as lower() and upper(). If we want to confirm if a string is in lowercase or uppercase, we can use islower() or isupper() functions. We can confirm this on interactive shell, see below.
If we have a list of strings that we want to join, join() can be used. We can also split a string using split() method. I will write a code showing how the split method works.
In a nutshell, Python provides various built-in functions and methods that can be used to manipulate strings to suit your needs. There are some methods which I didn’t mention in this piece, but I will provide a source that you can read and practice with the methods.
Source: AI Sweigart. Automate the Boring Stuff With Python, 2nd Edition
Comments
Post a Comment