Python String Methods You May Have Missed

John Au-Yeung - Feb 7 '20 - - Dev Community

Subscribe to my email list now at http://jauyeung.net/subscribe/

Follow me on Twitter at https://twitter.com/AuMayeung

Many more articles at https://medium.com/@hohanga

Even more articles at http://thewebdev.info/

Python is a convenient language that’s often used for scripting, data science, and web development.

In this article, we’ll look at how to use Python string methods to manipulate strings.

The upper(), lower(), isupper(), and islower() Methods

The upper method converts all characters of a string to upper case and returns it.

For instance, given the following string:

msg = 'Hello Jane'

Then running msg.upper() returns ‘HELLO JANE’ .

The lower method converts all characters of a string to lower case and returns it.

Therefore, msg.lower() returns ‘hello jane’ .

isupper checks if the whole string is converted to upper case.

For instance, if we have:

msg = 'HELLO JANE'

Then msg.isupper() returns True .

islower checks if the whole string is converted to lower case. For instance, given the following string:

msg = 'hello jane'

Then msg.islower() returns True .

upper and lower can be chained together since they both return strings.

For instance, we can write:

msg.upper().lower()

Then we get:

'hello jane'

returned.

The isX() Methods

There are also other methods for checking for various aspects of the string.

isalpha checks if the whole string consists of only letters and isn’t blank.

For instance, given the following string:

msg = 'hello jane'

Then msg.isalpha() returns False since it has a space in it.

isalnum checks is a string only consists of letters and numbers and returns True if it is.

For example, given the following string:

msg = 'hello'

Then msg.isalnum() returns True .

isdecimal returns True is string consists only of numeric characters and isn’t blank.

For instance, if we have:

msg = '12345'

Then msg.isdecimal() returns True .

isspace returns True if the string only consists of tabs, spaces, and newlines and isn’t blank.

For instance, if we have the following string:

msg = '\n '

Then msg.isspace() returns True .

istitle returns True if the string only has words that begin with an upper case letter followed by only lower case letters.

For instance, if we have the following string:

msg = 'Hello World'

Then msg.istitle() returns True .

The startswith() and endswith() Methods

The startswith method returns True if a string starts with the substring passed in as the argument.

For instance:

'Hello, world'.startswith('Hello')

returns True .

The endswith method returns True if a string ends with the substring passed in as the argument.

For instance:

'Hello, world!'.endswith('world!')

returns True since our string ends with world! .

The join() and split() Methods

The join method combines multiple strings in a string array into one string by the character that it’s called on.

For instance, we can write:

','.join(['apple', 'orange', 'grape'])

which returns ‘apple,orange,grape’.

The string that it’s called on is inserted between the entries.

The split method is used to split a string into a list of substrings by the character that it’s called on.

For instance:

'My name is Jane'.split(' ')

returns [‘My’, ‘name’, ‘is’, ‘Jane’] .

Splitting Strings with the partition() Method

The partition method splits a string into text before and after a separator string.

For instance:

'My name is Jane'.partition('is')

returns:

('My name ', 'is', ' Jane')

We can use the multiple assignment syntax to assign the parts into their own variables since the string is called on is always split into 3 parts.

For instance, we write the folllowing:

before, sep, after = 'My name is Jane'.partition('is')

Then before has the value ‘My name ‘ . sep is 'is' , and after is ' Jane' .

Justifying Text with the rjust(), ljust(), and center() Methods

The rjust method moves a string by the given number of spaces passed in as the argument to the right.

For instance:

'foo'.rjust(5)

returns:

'  foo'

It also takes a second argument to fill in something instead of spaces. For instance, ‘foo’.rjust(5, ‘-’) returns ‘--foo’

ljust adds spaces by the number of that’s passed into the argument to the right of the text.

For instance:

'foo'.ljust(5)

returns:

'foo  '

It also takes a second argument to fill in something instead of spaces. For instance, ‘foo’.ljust(5, ‘*’) returns ‘foo**’

The center method adds the number of spaces passed in as the argument to the left and the right of the string.

For instance:

'foo'.center(15)

returns:

'      foo      '

It also takes a second argument to fill in something instead of spaces. For instance, ‘foo’.center(5, ‘*’) returns ‘*foo*’ .

Conclusion

Python has string methods to convert strings to upper and lower case.

We can also add spaces and other characters to the string.

Multiple strings can also be joined together. Also, they can be split off into multiple strings.

There’re also many methods to check strings for various characteristics.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .