Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs
    • SQL FAQ’s

Python swapcase

by suresh

The Python swapcase function swaps the case of the characters and returns a new string. Or say, swapcase converts the Lowercase letters to Uppercase and Uppercase characters to lowercase. The syntax of the Python swapcase function is

String_Value.swapcase()

The swapcase Function is a Python String Method that keeps the Non-letters unchanged.

Python swapcase method Example

The following set of examples helps to know about the swapcase Function

# Python SwapCase Method Example

Str1 = 'Learn PYTHON AT TutOrial GateWay'
Str2 = Str1.swapcase()
print('First Output of a SwapCase() method is = ', Str2)

# Observe the Original String
print('Converted String is = ', Str1.swapcase())
print('Original String is = ', Str1)

# Performing Swapcase() function directly
Str3 = 'PYTHON ProGrammING'.swapcase()
print('Second Output of a SwapCase() method is = ', Str3)

Str4 = 'python1234TUTORIAL'.swapcase()
print('Third Output of a SwapCase() method is = ', Str4)

Str5 = '12345'.swapcase()
print('Fourth Output of a SwapCase() method is = ', Str5)

OUTPUT

Python SwapCase

ANALYSIS

The following swapcase statement converts the lowercase letters to uppercase and uppercase letters into lowercase.

Str2 = Str1.swapcase()
print('First Output of a SwapCase() method is = ', Str2)

The swapcase function returns the output in a new string, instead of altering the original string.

print('Converted String is = ', Str1.swapcase())
print('Original String is = ', Str1)

From the above swap case String function screenshot, see that the original string Str1 is unchanged. To change the original string, write the following Python statement.

Str1 = Str1.swapcase()

The swapcase converts the lowercase to upper and vice versa. However, it leaves other values unchanged. Here, we used the numeric values along with letters, and see the output for further reference.

Str4 = 'python1234TUTORIAL'.swapcase()
print('Third Output of a SwapCase() method is = ', Str4)

Str5 = '12345'.swapcase()
print('Fourth Output of a SwapCase() method is = ', Str5)

Placed Under: Python

  • Download and Install Python
  • Python Arithmetic Operators
  • Python Assignment Operators
  • Python Bitwise Operators
  • Python Comparison Operators
  • Python Logical Operators
  • Python If Statement
  • Python If Else
  • Python Elif Statement
  • Python Nested If
  • Python For Loop
  • Python While Loop
  • Python Break
  • Python Continue
  • Python Dictionary
  • Python datetime
  • Python String
  • Python Set
  • Python Tuple
  • Python List
  • Python List Comprehensions
  • Python Lambda
  • Python Functions
  • Python Types of Functions
  • Python Iterator
  • Python File
  • Python Directory
  • Python Class
  • Python classmethod
  • Python Inheritance
  • Python Method Overriding
  • Python Static Method
  • Connect Python and SQL Server
  • Python SQL Create DB
  • Python SQL Select Top
  • Python SQL Where Clause
  • Python SQL Order By
  • Python SQL Select Statement
  • Python len Function
  • Python max Function
  • Python map Function
  • Python print Function
  • Python sort Function
  • Python range Function
  • Python zip Function
  • Python Math Functions
  • Python String Functions
  • Python List Functions
  • Python NumPy Array
  • NumPy Aggregate Functions
  • NumPy Arithmetic Operations
  • Python Numpy Bitwise operators
  • Numpy Comparison Operators
  • Numpy Exponential Functions
  • Python Numpy logical operators
  • Python Numpy String Functions
  • NumPy Trigonometric Functions
  • Python random Array
  • Python Numpy concatenate
  • Python Numpy Array shape
  • Python Pandas DataFrame
  • Pandas DataFrame plot
  • Python Series
  • Python matplotlib Histogram
  • Python matplotlib Scatter Plot
  • Python matplotlib Pie Chart
  • Python matplotlib Bar Chart
  • Python List Length
  • Python sort List Function
  • Python String Concatenation
  • Python String Length
  • Python Substring
  • Python Programming Examples
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy