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.

HackerRank Python Solution - Built-Ins - ginortS

  • You are given a string S. S contains alphanumeric characters only.
  • Your task is to sort the string S in the following manner:
    • All sorted lowercase letters are ahead of uppercase letters.
    • All sorted uppercase letters are ahead of digits.
    • All sorted odd digits are ahead of sorted even digits.

HackerRank Python Solution - Built-Ins - Any or All

any():
  • This expression returns True if any element of the iterable is true. If the iterable is empty, it will return False.
Code:

>>> any([1>0,1==0,1<0])
True
>>> any([1<0,2<1,3<2])
False

HackerRank Python Solution - Built-Ins - Athlete Sort

  • You are given a spreadsheet that contains a list of N athletes and their details (such as age, height, weight, and so on). You are required to sort the data based on the Kth attribute and print the final resulting table. Follow the example given below for a better understanding.
  • Note that K is indexed from 0 to M-1, where M is the number of attributes.
  • Note: If two attributes are the same for different rows, for example, if two athletes are of the same age, print the row that appeared first in the input.

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...