Python Interviews: Difference between revisions

Line 111:
 
= Swap Case =
 
Python has built-in string validation methods for basic data. It can check if a string is composed of alphabetical characters, alphanumeric characters, digits, etc.
 
str.isalnum() checks if all the characters of a string are alphanumeric (a-z, A-Z and 0-9).
 
>>> print 'ab123'.isalnum()
True
 
str.isalpha() checks if all the characters of a string are alphabetical (a-z and A-Z).
 
>>> print 'abcD'.isalpha()
True
 
str.isdigit() checks if all the characters of a string are digits (0-9).
 
>>> print '1234'.isdigit()
True
 
str.islower() checks if all the characters of a string are lowercase characters (a-z).
 
>>> print 'abcd123#'.islower()
True
 
str.isupper() checks if all the characters of a string are uppercase characters (A-Z).
 
>>> print 'ABCD123#'.isupper()
True
 
 
<pre>
def swap_case(s):