Category: Strings
-
String Exercises
Example 1 Python program to find number of vowels in a given string. mystr =”All animals are equal. Some are more equal” vowels =”aeiou” count=0for x in mystr:if x.lower()in vowels: count+=1print(“Number of Vowels:”, count) It will produce the following output − Number of Vowels: 18 Example 2 Python program to convert a string with binary digits to…
-
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 thatcannotbe modified in…
-
Slicing Strings
Python String slicing is a way of creating a sub-string from a given string. In this process, we extract a portion or piece of a string. Usually, we use the slice operator “[ : ]” to perform slicing on a Python String. Before proceeding with string slicing let’s understand string indexing. In Python, a string is an ordered sequence…
-
Strings
In Python, a string is an immutable sequence of Unicode characters. Each character has a unique numeric value as per the UNICODE standard. But, the sequence as a whole, doesn’t have any numeric value even if all the characters are digits. To differentiate the string from numbers and other identifiers, the sequence of characters is included within single, double or triple…