HackerRankInterview QuestionsPythonPython InterviewRandom

HackerRank sWAP cASE problem solution in python | python problem solution


 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 .

Constraints

Sample Input 0

HackerRank.com presents "Pythonist 2".

Sample Output 0

hACKERrANK.COM PRESENTS "pYTHONIST 2".

Problem solution in Python 2 programming.

# Enter your code here. Read input from STDIN. Print output to STDOUT
name = raw_input()
print ''.join(c.lower() if c.isupper() else c.upper() for c in name)

Problem solution in Python 3 programming.

def swap_case(s):
return s.swapcase()

Problem solution in pypy programming.

def swap_case(s):
res = ''
for ch in s:
if ch.isupper():
res += ch.lower()
else:
res += ch.upper()
return res

Problem solution in pypy3 programming.

def swap_case(s):
result = []
for letter in s:
if letter == letter.lower():
result.append(letter.upper())
elif letter == letter.upper():
result.append(letter.lower())
else:
result.append(letter)
return ''.join(result)



Leave a ReplyCancel reply

Exit mobile version