
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python program to swap case of English word
Suppose we have a string with English letters. We have to swap the case of the letters. So uppercase will be converted to lower and lowercase converted to upper.
So, if the input is like s = "PrograMMinG", then the output will be pROGRAmmINg
To solve this, we will follow these steps −
- ret := blank string
- for each letter in s, do
- if letter is in uppercase, then
- ret := ret concatenate lower case equivalent of letter
- otherwise,
- ret := ret concatenate upper case equivalent of letter
- if letter is in uppercase, then
- return ret
Example
Let us see the following implementation to get better understanding
def solve(s): ret = '' for letter in s: if letter.isupper(): ret += letter.lower() else: ret += letter.upper() return ret s = "PrograMMinG" print(solve(s))
Input
"PrograMMinG"
Output
pROGRAmmINg
- Related Articles
- Java Program to swap the case of a String
- Program to swap string characters pairwise in Python
- Python Program to Swap the First and Last Value of a List
- Java program to display English alphabets
- Program to swap nodes in a linked list in Python
- Program to minimize hamming distance after swap operations in Python
- Python program to find word score from list of words
- Integer to English Words in Python
- Java Program to Swap Pair of Characters
- Python Program to Get word frequency in percentage
- Program to find Lexicographically Smallest String With One Swap in Python
- Python program to count occurrences of a word in a string
- Program to find length of longest diminishing word chain in Python?
- How to extract each (English) word from a string using regular expression in Java?
- Integer to English Words in Python Programming

Advertisements