HackerRank Python Solution - Strings - Text Alignment

  • In Python, a string of text can be aligned left, right and center.
.ljust(width):
  • This method returns a left-aligned string of length width.
>>> width = 20
>>> print 'HackerRank'.ljust(width,'-')
HackerRank----------

HackerRank Python Solution - Strings - String Validators

  • Python has built-in string validation methods for basic data. It can check if a string is composed of alphabetical characters, alphanumeric characters, digits, etc.
str.isalnum():
  • This method checks if all the characters of a string are alphanumeric (a-z, A-Z and 0-9).
>>> print 'ab123'.isalnum()
True
>>> print 'ab123#'.isalnum()
False

HackerRank Python Solution - Strings - Find a String

  • In this challenge, the user enters a string and a substring. You have to print the number of times that the substring occurs in the given string. String traversal will take place from left to right, not from right to left.
  • NOTE: String letters are case-sensitive.
Input Format:
  • The first line of input contains the original string. The next line contains the substring.

HackerRank Python Solution - Strings - Mutations

  • We have seen that lists are mutable (they can be changed), and tuples are immutable (they cannot be changed).
  • Let's try to understand this with an example.
  • You are given an immutable string, and you want to make changes to it.
Example:

>>> string = "abracadabra"

HackerRank Python Solution - Strings - What's Your Name?

You are given the firstname and lastname of a person on two different lines. Your task is to read them and print the following:
 
Hello firstname lastname! You just delved into python.
Function Description:
  • Complete the print_full_name function in the editor below.
  • print_full_name has the following parameters:
    • string first: the first name
    • string last: the last name
    • Prints string: 'Hello firstname lastname! You just delved into python' where and are replaced with first and last.

HackerRank Python Solution - Strings - Split and Join

In Python, a string can be split on a delimiter.

Example:

>>> a = "this is a string"
>>> a = a.split(" ") # a is converted to a list of strings. 
>>> print a
['this', 'is', 'a', 'string']
Joining a string is simple:
 
>>> a = "-".join(a)
>>> print a
this-is-a-string 

HackerRank Python Solution - Strings - sWAP cASE

You are given a string and your task is to swap cases. In other words, convert all lowercase letters to uppercase letters and vice versa.

For Example:

Www.HackerRank.com → wWW.hACKERrANK.COM
Pythonist 2 → pYTHONIST 2  

HackerRank Python Solution - Strings - Designer Door Mat

Mr. Vincent works in a doormat manufacturing company. One day, he designed a new doormat with the following specifications:
  • Mat size must be N x M. (N is an odd natural number, and M is 3 times N.)
  • The design should have 'WELCOME' written in the center.
  • The design pattern should only use |,. and - characters.

You might also like

Deploy your Django web app to Azure Web App using App Service - F1 free plan

In this post, we will look at how we can deploy our Django app using the Microsoft Azure app service - a free plan. You need an Azure accoun...