- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 10784 Articles for Python
![Mohd Mohtashim](https://www.tutorialspoint.com/assets/profiles/152429/profile/60_230655-1575021953.jpg)
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
![Mohd Mohtashim](https://www.tutorialspoint.com/assets/profiles/152429/profile/60_230655-1575021953.jpg)
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.
![Mohd Mohtashim](https://www.tutorialspoint.com/assets/profiles/152429/profile/60_230655-1575021953.jpg)
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
![Mohd Mohtashim](https://www.tutorialspoint.com/assets/profiles/152429/profile/60_230655-1575021953.jpg)
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
![Mohd Mohtashim](https://www.tutorialspoint.com/assets/profiles/152429/profile/60_230655-1575021953.jpg)
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
![Mohd Mohtashim](https://www.tutorialspoint.com/assets/profiles/152429/profile/60_230655-1575021953.jpg)
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
![Vikram Chiluka](https://www.tutorialspoint.com/assets/profiles/495654/profile/60_4081994-1659334126.jpg)
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
![Mohd Mohtashim](https://www.tutorialspoint.com/assets/profiles/152429/profile/60_230655-1575021953.jpg)
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
![Arnab Chakraborty](https://www.tutorialspoint.com/assets/profiles/123881/profile/60_2940367-1661162113.png)
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
![Arnab Chakraborty](https://www.tutorialspoint.com/assets/profiles/123881/profile/60_2940367-1661162113.png)
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