Posts

Showing posts with the label Strings

Python - How to Print String in Next Line Easily

Image
Here is an idea of how to split long strings into multiple lines. There are two methods - Backslash and Triple quotes. Python made handling strings easier for users. Assigning strings to a variable is accepted. That variable you can call an Object. Python treats everything as an Object. Method1: Backslash Method >>> s = "This is a test of the emergency broadcast system" \ … " and it will display an emergency if you put a line of text" \ … " that is more than 80 characters on a single line" >>> print(s) This is a test of the emergency broadcast system and it will display an emergency if you put a line of text that is more than 80 characters on a single line Method2: Triple Quote Method >>> s1 = """ This is a test of the emergency broadcast system. … and it will display an emergency if you put a line of text … that is more than 80 characters on a single line … """ >>> print(s1) This is a test

How to Explain String Immutability in Python Correctly

Image
Strings in Python are immutable. That means you can't change the string. So you can use strings in many places of your project securely. How to Test a String is Immutable It's very simple. In Python shell, assign a string to a variable of your choice. Here my choice is myString. Assigned to 'ABCD'. >>> myString = 'ABCD' >>>myString [0] = 'Z' It gives an error. Since you have tried to replace 'A' with 'Z'. It's because the string is immutable. Unlock Key Points This is the ultimate interview question When they ask String is immutable, you need to say 'Yes' Show them the example that I showed above Here are the Hidden Benefits of Strings Immutable It saves a lot of time in fixing and unchanging the storage requirement times. This directly results in high performance. String objects can be reused, as they are safely stored without getting changed. Strings in Python are considered as elemental as numbers. None of