DEV Community

Cover image for Python String Functions
ASHLEY KALONDU
ASHLEY KALONDU

Posted on

Python String Functions

Most data in the real world is string , and python offers a way to interact and transform data to enable us meet our analysis goals.

To check Type of Data

###1.Types

Type()

This method enables us to check the type/class of data held in our variables

Str()

This is a built in function that turns any type of data into a string.


in instances where you dont use f-strings you might to covert your data into text to match the rest of the data type and perform the print function

### Transformations
This methods aim to change the appearance or form of the text

Replace()

In this you are aiming to replace an existing text with a new value, in you syntax you should specify the old value and the new value / or replace it with nothing

split()

This aims to split strings to a list , you clarity the item to be used as the separation point and it will be replaced with commas (,)

Using + /Concat

We can use the plus sign to join similar data types
"Hello " + "World" results in "Hello World"

Join

Joins turns strings,sets,tuples into a single string, you outline the separator you want to be used
"SEPARATOR".join(list)

Multiplier

We use the asterisk that serves as the multiple sign.

Cleaning of Whitespaces

lstrip()

This methods clears any white space on the left side of the string(leading whitespaces)

rstrip

This method clear whitespace on the right side of the string (trailing spaces)

strip()

This method removes leading and trailing white spaces. It doesn't change the original string it returns a new string.
Strip() can also be used to remove special characters from the leading and trailing ends only.
Strip can't clear white spaces or special characters within the data.

Cleaning Cases

Lower()

Standardizes the string to lower case

Upper()

Standardizes the string to upper case

Title()

It capitalizes every first letter of all words

Searching

Startswith()

This checks the prefix of a string to check for matching and returns either True / False

Endwith()

This checks the suffix to check for matching and returns a boolean true/false

Find

This helps us find the index of the first occurrence of the substring, it returns -1 incase the substring isn't found

Integer

This checks the position of a substring in a string, performs similar to find but the difference is that if the substring doesn't exist it will crash .
The safe option is using find because incase the substring doesn't exist it gives a -1 feedback

In operator

In check if a value exists in a list, tuple, string
It returns True if found and False if the substring doesn't exist

Validation

This methods check if the strings meet certain conditions.
The feedback is boolean , either True or False

text.isalpha() Only letters
text.isdigit() Only digits
text.isalnum() Letters and numbers only
text.isnumeric() Numeric characters
text.isspace() Only spaces
text.islower() All letters are lowercase
text.isupper() All letters are uppercase
text.istitle() Each word starts with uppercase

Top comments (0)