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

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