Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Articles
Page 681 of 855
Unicode String in Python
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 MoreTriple Quotes in Python
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 MoreString Special Operators in Python
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 MoreUpdating Strings in Python
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 MoreAccessing Values of Strings in Python
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 MoreDecrypt String from Alphabet to Integer Mapping in Python
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 MoreMaximize Sum Of Array After K Negations in Python
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 MoreNon-decreasing Array in Python
A non-decreasing array is one where each element is less than or equal to the next element: array[i] nums[i+1] and count them ? If the array has 2 or fewer elements, return True Track violations using a boolean flag For each violation, decide whether to modify nums[i] or nums[i+1] If more than one violation exists, return False Implementation def checkPossibility(nums): if len(nums) nums[i + 1]: # If we already found a violation, return False ...
Read MoreShortest Unsorted Continuous Subarray in Python
Given an integer array, we need to find the shortest continuous subarray that, when sorted, makes the entire array sorted. For example, in the array [2, 6, 4, 8, 10, 9, 15], the subarray [6, 4, 8, 10, 9] needs to be sorted to make the whole array sorted, so the answer is 5. Approach We compare the original array with its sorted version to identify positions where elements differ. The shortest subarray spans from the first differing position to the last differing position. Algorithm Steps Create a sorted copy of the input array Compare ...
Read MorePalindrome Linked List in Python
A palindrome linked list is a linked list that reads the same forwards and backwards. For example, [1, 2, 3, 2, 1] forms a palindrome because it's identical when reversed. The most efficient approach uses the two-pointer technique with partial reversal to check palindrome in O(n) time and O(1) space. Algorithm Steps Use fast and slow pointers to find the middle of the linked list Reverse the first half while traversing to the middle Compare the reversed first half with the second half Return True if all elements match, False otherwise Implementation ...
Read More