Programming Essentials Python - Predefined Functions - String Manipulation Functions

Let us go through some of the important string manipulation functions using Python as the programming language.

Splitting Strings

If we want to generate a list of strings from a delimited string, we can use the split function. It splits the string into a list based on the specified delimiter.

user = '1,123 456 789,Scott,Tiger,1989-08-15,+1 415 891 9002,Forrest City,Texas,75063'
user.split(',')

Converting Case

You can convert strings to uppercase or lowercase using the upper() and lower() functions.

first_name = user.split(',')[2]
first_name.upper()
first_name.lower()

Concatenating Strings

You can concatenate strings using the + operator and capitalize the result.

full_name = (first_name + ' ' + last_name).capitalize()

Getting Substring

You can extract substrings from a string based on indices.

dob = user.split(',')[4]
dob[0:4]

Data Type Conversion

You can convert string data to other data types like integers or dates.

int(dob[0:4])
import datetime
datetime.datetime.strptime(user.split(',')[4], '%Y-%m-%d')

Hands-On Tasks

Explore the provided string and perform the following tasks:

  1. Split the string based on commas and store the result in a list.
  2. Convert the first name to uppercase and the last name to lowercase.
  3. Concatenate the first and last name and capitalize the full name.
  4. Extract the year part of the date of birth.
  5. Convert the year part to an integer.

Conclusion

In this article, we covered essential string manipulation functions in Python. Practice these tasks on your own to reinforce your understanding. Remember to engage with the community for further learning and support.

Watch the video tutorial here