Category: Tutorials
-
Add List Items
Add List Items Adding list items in Python implies inserting new elements into an existing list. Lists are mutable, meaning they can be modified after creation, allowing for the addition, removal, or modification of their elements. Adding items in a list typically refers to appending new elements to the end of the list, inserting them…
-
Change List Items
Change List Items List is a mutable data type in Python. It means, the contents of list can be modified in place, after the object is stored in the memory. You can assign a new value at a given index position in the list Syntax Example In the following code, we change the value at index 2 of…
-
Access List Items
Access List Items In Python, a list is a sequence of elements or objects, i.e. an ordered collection of objects. Similar to arrays, each element in a list corresponds to an index. To access the values within a list, we need to use the square brackets “[]” notation and, specify the index of the elements we want to retrieve. The…
-
Lists
Python Lists List is one of the built-in data types in Python. A Python list is a sequence of comma separated items, enclosed in square brackets [ ]. The items in a Python list need not be of the same data type. Following are some examples of Python lists − List is an ordered collection…
-
String Exercises
Example 1 Python program to find number of vowels in a given string. Open Compiler It will produce the following output − Example 2 Python program to convert a string with binary digits to integer. Open Compiler It will produce the following output − Change mystr to ’10, 101′ Example 3 Python program to drop all digits from a string. Open…
-
String Methods
Python’s built-in str class defines different methods. They help in manipulating strings. Since string is an immutable object, these methods return a copy of the original string, performing the respective processing on it. The string methods can be classified in following categories − Case Conversion Methods This category of built-in methods of Python’s str class deal with the conversion…
-
Escape Characters
Escape Character An escape character is a character followed by a backslash (\). It tells the Interpreter that this escape character (sequence) has a special meaning. For instance, \n is an escape sequence that represents a newline. When Python encounters this sequence in a string, it understands that it needs to start a new line. Unless an ‘r’ or…
-
String Formatting
String formatting in Python is the process of building a string representation dynamically by inserting the value of numeric expressions in an already existing string. Python’s string concatenation operator doesn’t accept a non-string operand. Hence, Python offers following string formatting techniques − Using % operator The “%” (modulo) operator often referred to as the string formatting…
-
String Concatenation
Concatenate Strings in Python String concatenation in Python is the operation of joining two or more strings together. The result of this operation will be a new string that contains the original strings. The diagram below shows a general string concatenation operation − In Python, there are numerous ways to concatenate strings. We are going to…
-
Modify Strings
String modification refers to the process of changing the characters of a string. If we talk about modifying a string in Python, what we are talking about is creating a new string that is a variation of the original one. In Python, a string (object of str class) is of immutable type. Here, immutable refers to an object that cannot be modified in…