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  
Function Description:
  • Complete the swap_case function in the editor below.
  • swap_case has the following parameters:
    • string s: the string to modify
    • Returns string: the modified string
Input Format:
  • A single line containing a string s.
Constraints:
  • 0 < len(s) <= 1000
Sample Input:

HackerRank.com presents "Pythonist 2".
Sample Output:

hACKERrANK.COM PRESENTS "pYTHONIST 2".
Solution:

def swap_case(s):
    ans=""
    for i in s:
        if i.isupper():
            ans+=i.lower()
        elif i.islower():
            ans+=i.upper()
        else:
            ans+=i
    return ans

if __name__ == '__main__':
    s = input()
    result = swap_case(s)
    print(result)    
Disclaimer: The problem statement is given by hackerrank.com but the solution is generated by the Geek4Tutorial admin. If there is any concern regarding this post or website, please contact us using the contact form. Thank you!

No comments:

Post a Comment

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