Python ProgrammingPython Programming

Convert Camel Case to Snake Case and Change Case of a particular character in a given string

Develop a short program to convert camel case in snake case. Change case of a particular character in a given string. swapcase used to change the upper case character into lower case and lower case character into uppercase.
import re
 
 
def convert(oldstring):
    s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', oldstring)
    return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower()
 
 
# Camel Case to Snake Case
print(convert('CamelCase'))
print(convert('CamelCamelCase'))
print(convert('getHTTPResponseCode'))
print(convert('get2HTTPResponseCode'))
 
# Change Case of a particular character
text = "python programming"
result = text[:1].upper() + text[1:7].lower() \
    + text[7:8].upper() + text[8:].lower()
print(result)
 
text = "Kilometer"
print(text.lower())
 
old_string = "hello python"
new_string = old_string.capitalize()
print(new_string)
 
old_string = "Hello Python"
new_string = old_string.swapcase()
print(new_string)
Sample output of above program.
camel_case
camel_camel_case
get_http_response_code
get2_http_response_code
Python Programming
kilometer
Hello python
hELLO pYTHON