- 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
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
655 Views
The problem we are trying to solve here is to convert CamelCase to separate out words. We can solve this directly using regexes by finding all occurrences of a capital letter in the given string and put a space before it. We can use the sub method from the re module.For example, for the input string −AReallyLongVariableNameInJavaWe should get the output −A Really Long Variable Name In JavaWe can use "[A-Z]" regex to find all uppercase letters, then replace them with space and that letter again. We can implement it using the re package as follows −Example Live Demoimport re ... Read More
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
343 Views
The easiest way to extract the maximum numeric value from a string using regex is to −Use the regex module to extract all the numbers from a stringFind the max from these numbersFor example, for the input string −There are 121005 people in this city, 1587469 in the neighboring city and 18775994 in a far-off city.We should get the output −18775994We can use "\d+" regex to find all numbers in a string as \d signifies a digit and the plus sign finds the longest string of continuous digits. We can implement it using the re package as follows −import re ... Read More
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
75 Views
We need to find the regex pattern 10+1 in a given string. For this, we can use the re module available in python. This package has a method called find all that accepts the regex and the string we want to search in. It gives us all the occurrences of the pattern in that string. For example,For the input string −10000001 hello world 10011 test100000001test.We should get the output −10000001 1001 100000001We can implement it using the re package as follows −import re occ = re.findall("10+1", "10000001 hello world 10011 test100000001test.") for i in occ: print(i)This will give the output −10000001 1001 100000001
![George John](https://www.tutorialspoint.com/assets/profiles/13511/profile/60_58410-1512537824.png)
9K+ Views
You can assign a dictionary value to a variable in Python using the access operator []. For example,Examplemy_dict = { 'foo': 42, 'bar': 12.5 } new_var = my_dict['foo'] print(new_var)OutputThis will give the output −42This syntax can also be used to reassign the value associated with this key. For example,Examplemy_dict = { 'foo': 42, 'bar': 12.5 } my_dict['foo'] = "Hello" print(my_dict['foo'])OutputThis will give the output −Hello
![Vikram Chiluka](https://www.tutorialspoint.com/assets/profiles/495654/profile/60_4081994-1659334126.jpg)
2K+ Views
In this article, we will show you how to remove the same element in the list in python. In simple words removing common elements from both lists. Below are the various methods to accomplish this task − Using remove() function Using List Comprehension Using Set difference operator Using Set difference() function Assume we have taken two lists containing some elements. We will now remove the same or common elements from both lists. Using remove() function Algorithm (Steps) Following are the Algorithm/steps to be followed to perform the desired task − Create a variable to store the inputList_1. ... Read More
![Arnab Chakraborty](https://www.tutorialspoint.com/assets/profiles/32020/profile/60_153038-1524898298.jpg)
61 Views
The first problem u are getting in the bold portion is due to non-indent block, put one indentation there.second problem is name variable is not definedfollowing is the corrected one -print ("Come-on in. Need help with any bags?") bag=input ('(1) Yes please (2) Nah, thanks (3) Ill get em later TYPE THE NUMBER ONLY') if bag == ('1'): print ("Ok, ill be right there!") if bag == ('2'): print ("Okee, see ya inside. Heh, how rude of me? I'm Daniel by the way, ya?") name="Daniel" print (name + ": Um, Names " + name) print ("Dan: K, nice too ... Read More
![Arnab Chakraborty](https://www.tutorialspoint.com/assets/profiles/32020/profile/60_153038-1524898298.jpg)
2K+ Views
You can solve this problem by using if-elif-else statements. And to make it like, it will ask for a valid option until the given option is on the list, we can use while loops. When the option is valid, then break the loop, otherwise, it will ask for the input repeatedly.You should take the input as an integer, for that you need to typecast the input to an integer using int() method.ExamplePlease check the code to follow the given points.print("Come-on in. Need help with any bags?") while True: # loop is used to take option until it is not valid. ... Read More
![Pythonista](https://www.tutorialspoint.com/assets/profiles/15003/profile/60_128639-1514576467.png)
1K+ Views
Usin count() function in itertools module gives an iterator of evenly spaced values. The function takes two parameters. start is by default 0 and step is by default 1. Using defaults will generate infinite iterator. Use break to terminate loop.import itertools percentNumbers = [ ] finish = "n" num = "0" for x in itertools.count() : num = input("enter the mark : ") num = float(num) percentNumbers.append(num) finish = input("stop? (y/n) ") if finish=='y':break print(percentNumbers)Sample output of the above scriptenter the mark : 11 stop? (y/n) enter the mark : 22 stop? (y/n) enter the mark : 33 stop? (y/n) y [11.0, 22.0, 33.0]
![Arjun Thakur](https://www.tutorialspoint.com/assets/profiles/13574/profile/60_74441-1560315595.jpg)
122 Views
You can create an array to keep track of all non digit characters in a string. Then finally join this array using "".join method. examplemy_str = 'qwerty123asdf32' non_digits = [] for c in my_str: if not c.isdigit(): non_digits.append(c) result = ''.join(non_digits) print(result)OutputThis will give the outputqwertyasdfexampleYou can also achieve this using a python list comprehension in a single line. my_str = 'qwerty123asdf32' result = ''.join([c for c in my_str if not c.isdigit()]) print(result)OutputThis will give the outputqwertyasdf
![Priya Pallavi](https://www.tutorialspoint.com/assets/profiles/13562/profile/60_91320-1512544134.jpg)
487 Views
You can use the following code to find if a number is a keith number in python −Exampledef is_keith_number(n): # Find sum of digits by first getting an array of all digits then adding them c = str(n) a = list(map(int, c)) b = sum(a) # Now check if the number is a keith number # For example, 14 is a keith number because: # 1+4 = 5 # 4+5 = 9 # 5+9 = 14 while b < n: a = a[1:] + [b] b = sum(a) return (b == n) & (len(c) > 1) print(is_keith_number(14))OutputThis will give the output −True