Modify Strings
Strings in Python are immutable, which means you cannot change the contents of a string once it is created. However, you can create modified copies of a string by applying various string methods.
Changing Case
You can change the case of a string using the toUpperCase()
andtoLowerCase()
methods. For example:
let text = 'Hello, World!';
const uppercaseText = text.toUpperCase();
const lowercaseText = text.toLowerCase();
console.log(uppercaseText); // Output: 'HELLO, WORLD!'
console.log(lowercaseText); // Output: 'hello, world!'
Replacing Substrings
You can replace a substring in a string using the replace()
method. For example:
let text = 'Hello, World!';
const replacedText = text.replace('World', 'Python');
console.log(replacedText); // Output: 'Hello, Python!'
Conclusion
Although strings in Python are immutable, you can create modified versions of strings using various string methods. By understanding how to change case and replace substrings, you can manipulate strings to fit your needs in your Python programs.