Press "Enter" to skip to content

Python Concatenate Strings

String manipulation is helpful for:

  • Cleaning the dataset to prepare it for text mining or sentiment analysis.
  • Process email content to feed a machine learning algorithm which decides whether an email is spam.
  • Parse and extract specific data from a website to build a database.
my_string = "This is a string"
my_string2 = 'This is also a string'

You can find the length of the string using the len keyword:

my_string = "Awesome day"
len(my_string)
11

A palindrome is a sequence of characters which can be read the same backward as forward, for example: Madam or No lemon, no melon. You realize that there are some funny movie names that can have this characteristic. You want to make a list of all movie titles that are funny palindromes but you will start by analyzing one example.

In python, you can also specify steps by using a third index. If you don’t specify the first or second index and the third one is negative, it will return the characters jumping and backwards.

Exercise

TRY IT YOURSELF: Access the exercise in our Regular Expressions in Python course here.

  • Extract the substring from the 12th to the 30th character from the variable movie which corresponds to the movie title. Store it in the variable movie_title.
  • Get the palindrome by reversing the string contained in movie_title.
  • Complete the code to print out the movie_title if it is a palindrome.
# Get the word
movie_title = ____[____] # Obtain the palindrome
palindrome = ____[____] # Print the word if it's a palindrome
if movie_title == palindrome: print(____)

Video

To learn more about string manipulation, please see this video from our course Regular Expressions in Python.

[embedded content]

This content is taken from DataCamp’s Regular Expressions in Python course by Maria Eugenia Inzaugarat.

Be First to Comment

Leave a Reply

Your email address will not be published.