Found 10784 Articles for Python

Built-in String Methods in Python

Mohd Mohtashim
Updated on 28-Jan-2020 12:31:33

3K+ Views

Python includes the following built-in methods to manipulate strings −Sr.NoFunction & Description1capitalize()Capitalizes first letter of string2center(width, fillchar)Returns a space-padded string with the original string centered to a total of width columns.3count(str, beg= 0, end=len(string))Counts how many times str occurs in string or in a substring of string if starting index beg and ending index end are given.4decode(encoding='UTF-8', errors='strict')Decodes the string using the codec registered for encoding. encoding defaults to the default string encoding.5encode(encoding='UTF-8', errors='strict')Returns encoded string version of string; on error, default is to raise a ValueError unless errors is given with 'ignore' or 'replace'.6endswith(suffix, beg=0, end=len(string))Determines if string or ... Read More

Unicode String in Python

Mohd Mohtashim
Updated on 28-Jan-2020 12:30:51

3K+ Views

Normal strings in Python are stored internally as 8-bit ASCII, while Unicode strings are stored as 16-bit Unicode. This allows for a more varied set of characters, including special characters from most languages in the world. I'll restrict my treatment of Unicode strings to the following −Example Live Demo#!/usr/bin/python print u'Hello, world!'OutputWhen the above code is executed, it produces the following result −Hello, world! As you can see, Unicode strings use the prefix u, just as raw strings use the prefix r.

Triple Quotes in Python

Mohd Mohtashim
Updated on 28-Jan-2020 12:22:40

6K+ Views

Python's triple quotes comes to the rescue by allowing strings to span multiple lines, including verbatim NEWLINEs, TABs, and any other special characters.The syntax for triple quotes consists of three consecutive single or double quotes.Example Live Demo#!/usr/bin/python para_str = """this is a long string that is made up of several lines and non-printable characters such as TAB ( \t ) and they will show up that way when displayed. NEWLINEs within the string, whether explicitly given like this within the brackets [ ], or just a NEWLINE within the variable assignment will also show up. """ print para_strOutputWhen the above code is ... Read More

String Special Operators in Python

Mohd Mohtashim
Updated on 28-Jan-2020 12:21:20

2K+ Views

Assume string variable a holds 'Hello' and variable b holds 'Python', then −Sr.NoOperator & DescriptionExample1+Concatenation - Adds values on either side of the operatora + b will give HelloPython2*Repetition - Creates new strings, concatenating multiple copies of the same stringa*2 will give-HelloHello3[]Slice - Gives the character from the given indexa[1] will give e4[ : ]Range Slice - Gives the characters from the given rangea[1:4] will give ell5inMembership - Returns true if a character exists in the given stringH in a will give 16not inMembership - Returns true if a character does not exist in the given stringM not in a ... Read More

Updating Strings in Python

Mohd Mohtashim
Updated on 28-Jan-2020 12:20:12

2K+ Views

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. For example −Example Live Demo#!/usr/bin/python var1 = 'Hello World!' print "Updated String :- ", var1[:6] + 'Python'OutputWhen the above code is executed, it produces the following result −Updated String :- Hello Python

Accessing Values of Strings in Python

Mohd Mohtashim
Updated on 28-Jan-2020 11:44:45

1K+ Views

Python does not support a character type; these are treated as strings of length one, thus also considered a substring.ExampleTo access substrings, use the square brackets for slicing along with the index or indices to obtain your substring. For example − Live Demo#!/usr/bin/python var1 = 'Hello World!' var2 = "Python Programming" print "var1[0]: ", var1[0] print "var2[1:5]: ", var2[1:5]OutputWhen the above code is executed, it produces the following result −var1[0]: H var2[1:5]: ytho

Mathematical Constants in Python

Vikram Chiluka
Updated on 25-Oct-2022 08:30:08

854 Views

In this article, we will go through Python's mathematical constants and how to use them. Some of the defined constants that can be utilized in a variety of mathematical operations are included in the math module. These mathematical constants return values that are equivalent to their standard defined values. The following math module constants are available in the Python programming language: Python math.e constant Python math.pi constant Python math.tau constant Python math.inf constant Python math.nan constant Python math.e constant The Euler's number, 2.71828182846, is returned by the math.e constant. Syntax math.e Return Value − It returns the ... Read More

Random Number Functions in Python

Mohd Mohtashim
Updated on 28-Jan-2020 11:43:01

440 Views

Random numbers are used for games, simulations, testing, security, and privacy applications. Python includes following functions that are commonly used.Sr.NoFunction & Description1choice(seq)A random item from a list, tuple, or string.2randrange ([start, ] stop [, step])A randomly selected element from range(start, stop, step)3random()A random float r, such that 0 is less than or equal to r and r is less than 14seed([x])Sets the integer starting value used in generating random numbers. Call this function before calling any other random module function. Returns None.5shuffle(lst)Randomizes the items of a list in place. Returns None.6uniform(x, y)A random float r, such that x is less ... Read More

Decrypt String from Alphabet to Integer Mapping in Python

Arnab Chakraborty
Updated on 27-Apr-2020 09:18:50

646 Views

Suppose we have a string s that is formed by digits ('0' - '9') and '#'. We have to map s to one English lowercase characters as follows −Characters ('a' to 'i') are represented by ('1' to '9') respectively.Characters ('j' to 'z') are represented by ('10#' to '26#') respectively.We have to find the string formed after mapping. We are taking one assumption that a unique mapping will always exist. So if the input is like “10#11#12”, then it will be “jkab”. As 10# is j, 11# is k, 1 is a and 2 is b.To solve this, we will follow ... Read More

Maximize Sum Of Array After K Negations in Python

Arnab Chakraborty
Updated on 27-Apr-2020 09:06:24

244 Views

Suppose we have an array A of integers, we have to modify the array in the following way −We can choose an i and replace the A[i] with -A[i], and we will repeat this process K times. We have to return the largest possible sum of the array after changing it in this way.So, if the array A = [4, 2, 3], and K = 1, then the output will be 5. So choose indices 1, the array will become [4, -2, 3]To solve this, we will follow these steps −Sort the array Afor i in range 0 to length ... Read More

Advertisements