In Python, you can adjust the case of strings using the upper() and lower() methods.
The upper() method converts all characters in a string to uppercase:
text = "hello, world!"uppercase_text = text.upper()print(uppercase_text) # Output: "HELLO, WORLD!" |
The lower() method converts all characters in a string to lowercase:
text = "HELLO, WORLD!"lowercase_text = text.lower()print(lowercase_text) # Output: "hello, world!" |
You can also adjust the case of specific characters in a string using slicing and concatenation. For example, to capitalize the first letter of a string, you can use:
text = "hello, world!"capitalized_text = text[0].upper() + text[1:]print(capitalized_text) # Output: "Hello, world!" |
Here, we slice the first character of the string (text[0]) and capitalize it using the upper() method. We then concatenate this capitalized character with the rest of the string (text[1:]), which we leave unchanged.
Similarly, you can lowercase the first letter of a string using:
|
text = "Hello, world!"
lowercase_text = text[0].lower() + text[1:]
print(lowercase_text) # Output: "hello, world!"
|
Here, we lowercase the first character of the string (text[0]) using the lower() method and concatenate it with the rest of the string (text[1:]), which we leave unchanged.