Found 33676 Articles for Programming

The most occurring number in a string using Regex in python

Hafeezul Kareem
Updated on 11-Jul-2020 08:18:27

233 Views

In this tutorial, we are going to write a regex that finds the most occurring number in the string. We will check the regex in Python.Follow the below steps to write the program.Import the re and collections modules.Initialize the string with numbers.4Find all the numbers using regex and store them in the array.Find the most occurring number using Counter from collections module.Example Live Demo# importing the modules import re import collections # initializing the string string = '1222tutorials321232point3442' # regex to find all the numbers regex = r'[0-9]' # getting all the numbers from the string numbers = re.findall(regex, string) # ... Read More

Taking multiple inputs from user in Python

Hafeezul Kareem
Updated on 11-Jul-2020 08:16:43

1K+ Views

In this tutorial, we are going to learn how to take multiple inputs from the user in Python.The data entered by the user will be in the string format. So, we can use the split() method to divide the user entered data.Let's take the multiple strings from the user.Example# taking the input from the user strings = input("Enter multiple names space-separated:- ") # spliting the data strings = strings.split() # printing the data print(strings)OutputIf you run the above code, then you will get the following result.Enter multiple names space-separated:- Python JavaScript Django React ['Python', 'JavaScript', 'Django', 'React']What if we want ... Read More

Taking input from console in Python

Hafeezul Kareem
Updated on 11-Jul-2020 08:14:12

3K+ Views

In this tutorial, we are going to learn how to take input from the console in Python.The interactive shell in Python is treated as a console. We can take the user entered data the console using input() function.Example# taking input from the user a = input() # printing the data print("User data:-", a) Tutorialspoint User data:- TutorialspointOutputIf you run the above code, then you will get the following result.Tutorialspoint User data:- TutorialspointConclusionThe data that is entered by the user will be in the string format. If you have any doubts in the tutorial, mention them in the comment section.Read More

Take Matrix input from user in Python

Hafeezul Kareem
Updated on 11-Jul-2020 08:09:51

9K+ Views

In this tutorial, we are going to learn how to take matric input in Python from the user. We can take input from the user in two different ways. Let's see two of them.Method 1Taking all numbers of the matric one by one from the user. See the code below.Example# initializing an empty matrix matrix = [] # taking 2x2 matrix from the user for i in range(2):    # empty row    row = []    for j in range(2):       # asking the user to input the number       # converts the input to ... Read More

Swap two variables in one line in C/C++, Python, PHP and Java

Hafeezul Kareem
Updated on 11-Jul-2020 08:06:36

620 Views

In this tutorial, we are going to learn how to swap two variables in different languages. Swapping means interchanging the values of two variables. Let's see an example.Inputa = 3 b = 5Outputa = 5 b = 3Let's see one by one.PythonWe can swap the variable with one line of code in Python. Let's see the code.Example Live Demo# initializing the variables a, b = 3, 5 # printing before swaping print("Before swapping:-", a, b) # swapping a, b = b, a # printing after swapping print("After swapping:-", a, b)OutputIf you run the above code, then you will get the following ... Read More

Sum of list (with string types) in Python

Hafeezul Kareem
Updated on 11-Jul-2020 08:00:30

3K+ Views

In this tutorial, we are going to write a program that adds all numbers from the list. List may contain numbers in string or integer format. See the example.Inputrandom_list = [1, '10', 'tutorialspoint', '2020', 'tutorialspoint@2020', 2020]Output4051Follow the below steps to write the program.Initialize the list.3Initialize a variable total with 0.Iterate over the list.If the element is int, then add it to the total by checking two conditions.The element will be int -> Check type.The element will be a number in string format -> Check using isdigit() method.Print the totalExample Live Demo# initialzing the list random_list = [1, '10', 'tutorialspoint', '2020', 'tutorialspoint@2020', ... Read More

Structuring Python Programs

Hafeezul Kareem
Updated on 11-Jul-2020 07:59:10

435 Views

In this tutorial, we are going to see some best practices for structuring the Python programs. Let's see one by oneUse a tab for indentationUsing a tab for indentation in the code make code more readable instead of using random spaces for multiple functions and methods. You can set the number of spaces for a tab in any code editors' settings.Example# example def sample(random): # statement 1 # statement 2 # ... return randomDon't write more than 79 characters in a lineWriting more than 79 characters in a line is not recommended Python. Avoid this by breaking line into multiple ... Read More

struct module in Python

Hafeezul Kareem
Updated on 11-Jul-2020 07:56:56

4K+ Views

In this tutorial, we are going to learn about the struct module. Let's dive into the tutorial.The module struct is used to convert the native data types of Python into string of bytes and vice versa. We don't have to install it. It's a built-in module available in Python3.The struct module is related to the C languages. We have to know the notations used in C to represent various data type to work with struct module. Let's see some of them.Data TypeFormat CharacterinticharcstringsfloatfLet's see how to convert the Python data types into bytes.struct.pack()The method struct.pack() is used to convert the ... Read More

string.whitespace in Python

Hafeezul Kareem
Updated on 11-Jul-2020 07:56:31

398 Views

In this tutorial, we are going to learn about the string.whitespace.The string whitespace is pre-defined in the string module of Python3. If contains space, tab, linefeed, return, formfeed, and vertical tab.Example Live Demo# importing the string module import string # printing a string print("Hello") # whitespace printing print(string.whitespace) # printing a string print('tutorialspoint')OutputIf you run the above code, then you will get the following result.Hello tutorialspointConclusionIf you have any doubts in the tutorial, mention them in the comment section.

string.punctuation in Python

Hafeezul Kareem
Updated on 11-Jul-2020 07:54:14

4K+ Views

In this tutorial, we are going to learn about the string.punctuation string.The string punctuation is pre-defined in the string  module of Python3. It contains all the characters as a string. We can use it anywhere in the program.Example Live Demo# importing the string module import string # printing the punctuation print(string.punctuation)OutputIf you run the above code, then you will get the following result.!"#$%&'()*+,-./:;?@[\]^_`{|}~ConclusionWe can use it to generate strong passwords. Try it out. If you have any doubts in the tutorial, mention them in the comment section.

Advertisements