Python provides numerous built-in functions and methods to work with lists efficiently. These functions help perform common operations like finding length, maximum/minimum values, and converting sequences to lists. Built-in List Functions Python includes the following list functions ? Sr.No Function with Description ... Read More
Lists are sequence objects in Python that support the same fundamental operations as strings. They respond to the + and * operators for concatenation and repetition, except the result is a new list rather than a string. Lists support all general sequence operations, making them versatile for data manipulation and processing tasks. Length Operation Use len() to get the number of elements in a list − numbers = [1, 2, 3] print("Length of list:", len(numbers)) Length of list: 3 Concatenation with + Operator Combine two lists using the + ... Read More
Python provides a rich set of built-in string methods for manipulating and working with text data. These methods cover everything from case conversion and formatting to searching and validation. Case Conversion Methods Methods for changing the case of characters in strings ? text = "hello WORLD" print("Original:", text) print("capitalize():", text.capitalize()) print("upper():", text.upper()) print("lower():", text.lower()) print("title():", text.title()) print("swapcase():", text.swapcase()) Original: hello WORLD capitalize(): Hello world upper(): HELLO WORLD lower(): hello world title(): Hello World swapcase(): HELLO world String Validation Methods Methods that return True or False based on string content ... Read More
In Python, Unicode strings allow you to work with characters from various languages and special symbols. While Python 2 required the u prefix for Unicode strings, Python 3 treats all strings as Unicode by default. Unicode in Python 3 In Python 3, all strings are Unicode by default, so no special prefix is needed ? # All strings are Unicode in Python 3 text = 'Hello, world! 你好 مرحبا' print(text) print(type(text)) Hello, world! 你好 مرحبا Working with Unicode Characters You can use Unicode escape sequences to represent special characters ... Read More
Python's triple quotes allow strings to span multiple lines, including verbatim newlines, tabs, and any other special characters. This feature is particularly useful for multi-line strings, docstrings, and preserving text formatting. The syntax for triple quotes consists of three consecutive single (''') or double (""") quotes. Syntax # Using triple double quotes text = """Multi-line string content""" # Using triple single quotes text = '''Multi-line string content''' Basic Multi-line String Example Triple quotes preserve all whitespace and special characters within the string ? para_str = """This is a ... Read More
Python provides several special operators for working with strings. These operators allow you to concatenate, repeat, slice, and format strings efficiently. Let's explore each operator with practical examples using string variables a = 'Hello' and b = 'Python'. String Concatenation (+) The + operator joins two or more strings together ? a = 'Hello' b = 'Python' result = a + b print(result) print(a + ' ' + b) HelloPython Hello Python String Repetition (*) The * operator creates multiple copies of a string ? a = 'Hello' ... Read More
You can "update" an existing string by (re)assigning a variable to another string. The new value can be related to its previous value or to a completely different string altogether. String Reassignment Since strings are immutable in Python, you cannot modify them directly. Instead, you create a new string ? var1 = 'Hello World!' var1 = 'Hello Python!' print("Updated String:", var1) Updated String: Hello Python! Combining Parts of Original String You can create a new string by combining parts of the original string with new content ? var1 ... Read More
Python does not support a character type; these are treated as strings of length one, thus also considered a substring. To access individual characters or substrings, use square brackets with the index or slice notation. Accessing Individual Characters Use square brackets with an index to access a single character. Python uses zero-based indexing ? var1 = 'Hello World!' var2 = "Python Programming" print("var1[0]:", var1[0]) print("var1[6]:", var1[6]) print("var2[0]:", var2[0]) print("var2[-1]:", var2[-1]) # Last character var1[0]: H var1[6]: W var2[0]: P var2[-1]: g Accessing Substrings with Slicing Use slice notation ... Read More
Suppose we have a string s that contains digits ('0' - '9') and '#' characters. We need to map this string to English lowercase characters using a specific encoding scheme ? Characters ('a' to 'i') are represented by ('1' to '9') respectively. Characters ('j' to 'z') are represented by ('10#' to '26#') respectively. For example, if the input is "10#11#12", the output will be "jkab" because 10# maps to 'j', 11# maps to 'k', 1 maps to 'a', and 2 maps to 'b'. Approach We'll solve this by processing the string from right to ... Read More
Given an array of integers, we need to maximize the sum by negating exactly K elements. We can choose any element and replace it with its negative value, repeating this process K times. The strategy is to first negate all negative numbers (since negating them increases the sum), then handle remaining negations optimally. Algorithm The approach involves these steps: Sort the array to process negative numbers first Negate negative numbers until K operations are exhausted or no negatives remain If K operations remain and K is odd, negate the smallest positive number Return the sum ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance