Python String methods

python-string-methods-image

Text or String is one of the most common data type that we have to work as a software developer. In this tutorial, we’re gonna look at almost Python String methods which are important when working with text.

Create raw String with escape characters

We use r before quotation mark of a string:

Create multi-lines String

When we put string inside three single quotes/ three double quotes, any quote, tab, or newline will be considered part of the string.

For example:

Or we can test using a .py file with the code below and run it:

Output:

Get characters by Index and Slide String

We can apply indexes and slides on Python String with the same way as Python List. Just think of a string as a list and each character in the string as an item:

Python Transform String methods

Upper and Lower String

upper() and lower() methods return a new string (not change the original string) with all letters have been converted to uppercase or lowercase:

Swap Upper-Lower String

swapcase() returns a new string that converts all uppercase characters to lowercase and all lowercase characters to uppercase characters.

Capitalize first letter

capitalize() converts first letter of a string to uppercase letter and lowercases others:

title() returns a string with first letter of each word capitalized:

Python Validate String methods

Uppercase & Lowercase

isupper() and islower() methods return:
True if:
+ the string has at least one letter (not number)
+ all the letters are uppercase/lowercase
False for other cases

For example:

Letters & Numbers

isalpha() returns True if the string has only letters (not blank).

isalnum() returns True if the string has only letters and numbers (not blank).

isdecimal() returns True if the string has only numeric characters (not blank).

Space

isspace() returns True if the string has only spaces, tabs, and newlines (not blank).

Title type

istitle() returns True if the string has only words that begin with an uppercase letter followed by only lowercase letters.

Start/End with

startswith() returns True if the string value begins with the string passed to the method:

endswith() returns True if the string value ends with the string passed to the method:

Python Join and Split String methods

Join strings

We use join() to join a list of string into a single string value. The returned string is the concatenation of each string in the passed-in list.

Split string

split() is called on a string value and returns a list of strings.

By default, the string is split wherever a whitespace characters (space, tab, or newline) is found. We can pass a delimiter string to the split() method to specify the string to split upon.

Partition string

partition() gets argument string as a separator, then:
– splits the string at the first occurrence of separator
– returns a tuple containing 3 parts: the string before separator, separator, and the part after separator.

Python Justify Text

Right Justified

rjust() returns a right-justified string with given minimum width.

By default, spaces will be filled on the left. We can specify a fill character with second argument:

Left Justified

ljust() returns a left-justified string with given minimum width.

By default, spaces will be filled on the right. We can specify a fill character with second argument:

Center

center() works like ljust() and rjust(), but centers the text rather than justifying it to the left or right.

Strip off characters

strip() returns a new string without any whitespace characters at the beginning or end:

lstrip() removes whitespace characters from the left, and rstrip() removes whitespace characters from the right ends:

We can pass a string argument to this method to specify the set of characters to be removed:

*Note: The order of the characters in the string passed to strip() does not matter: strip('sTut') will do the same thing as strip('tTus') or strip('Tuts').

Check Substring in String

We can know whether a string is or is not in a parent string using in or not in operators:

Python String Finding methods

Count Substrings

count() returns the number of substrings inside the given string.

Find index of Substring
index() & rindex()

index() returns the index of first occurrence of the substring (if found) in the string, or throw an exception when the substring is not found.

rindex() returns the index of last occurrence of the substring (if found) in the string, or throw an exception when the substring is not found.

find() & rfind()

– Similar to index()/rindex() method, but there is only one difference: find()/rfind() return -1 if not found.

Find and Replace

replace() returns the string in which, every substring is replaced with argument string.



By grokonez | December 27, 2018.


Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*