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 661 of 855
Taking multiple inputs from user in Python
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. Taking Multiple String Inputs Let's take multiple strings from the user using split() ? # taking the input from the user strings = input("Enter multiple names space-separated:- ") # splitting the data strings = strings.split() # printing the data print(strings) The output of the above code is ? ...
Read MoreTake Matrix input from user in Python
In this tutorial, we will learn how to take matrix input from the user in Python. There are two common approaches: taking individual elements one by one, or taking entire rows with space-separated values. Method 1: Taking Individual Elements This method asks the user to input each matrix element individually ? 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): ...
Read MoreSum of list (with string types) in Python
In this tutorial, we'll write a program that adds all numbers from a list containing mixed data types. The list may contain numbers in string or integer format, along with non-numeric strings. Problem Statement Given a list with mixed data types, we need to sum only the numeric values (both integers and numeric strings) while ignoring non-numeric strings ? Input random_list = [1, '10', 'tutorialspoint', '2020', 'tutorialspoint@2020', 2020] Expected Output 4051 Solution Approach Follow these steps to solve the problem: Initialize the list with mixed data ...
Read MoreStructuring Python Programs
In this tutorial, we are going to see some best practices for structuring Python programs. Following these conventions makes your code more readable and maintainable. Use Consistent Indentation Python uses indentation to define code blocks. Use 4 spaces per indentation level as recommended by PEP 8. Avoid mixing tabs and spaces ? def calculate_area(length, width): # 4 spaces for indentation if length > 0 and width > 0: area = length * width ...
Read Morestruct module in Python
The struct module in Python converts native Python data types into strings of bytes and vice versa. It's a built-in module that uses C-style format characters to specify data types and their binary representation. Format Characters The struct module uses format characters similar to C language data types ? Data Type Format Character Description int i 4-byte signed integer char c 1-byte character string s char[] (requires length prefix) float f 4-byte floating point struct.pack() - Converting to Bytes The struct.pack() method converts ...
Read Morestring.whitespace in Python
In this tutorial, we are going to learn about string.whitespace in Python. The string.whitespace constant is pre-defined in the string module and contains all ASCII whitespace characters: space, tab, linefeed, return, formfeed, and vertical tab. What is string.whitespace? The string.whitespace is a string constant that contains all characters considered as whitespace in ASCII. This includes: Space character (' ') Tab character ('\t') Newline character ('') Carriage return ('\r') Form feed ('\f') Vertical tab ('\v') Basic Example Let's see what string.whitespace contains ? import string print("Content of string.whitespace:") print(repr(string.whitespace)) print("Length:", len(string.whitespace)) ...
Read Morestring.punctuation in Python
In this tutorial, we are going to learn about the string.punctuation constant in Python. The string.punctuation is a pre-defined string constant in Python's string module that contains all ASCII punctuation characters. We can use it for text processing, password generation, and data validation. What is string.punctuation? The string.punctuation constant contains all printable ASCII punctuation characters as a single string ? import string # Display the punctuation string print("Punctuation characters:") print(string.punctuation) print(f"Total characters: {len(string.punctuation)}") Punctuation characters: !"#$%&'()*+, -./:;?@[\]^_`{|}~ Total characters: 32 Practical Examples Removing Punctuation from Text Remove all ...
Read Morestring.octdigits in Python
In this tutorial, we are going to learn about the string.octdigits constant in Python. The string.octdigits is a pre-defined string constant in the string module of Python. It contains all the octal digits (0-7) as a single string. We can use this constant whenever we need to work with octal digits in our program by simply importing it from the string module. Basic Usage Let's see what string.octdigits contains ? import string # Print the octal digits string print(string.octdigits) print("Length:", len(string.octdigits)) 01234567 Length: 8 Checking the Data Type ...
Read MoreSQL using Python
In this tutorial, we are going to learn how to use SQL with Python using SQLite database. Python has a built-in module to connect with SQLite database. We are going to use sqlite3 module to connect Python and SQLite. We have to follow the below steps to connect the SQLite database with Python ? Import the sqlite3 module. Create a connection using the sqlite3.connect(db_name) method that takes a database name as an argument. It creates one file if it doesn't exist with the given name else it opens the file with ...
Read MoreSplit a string in equal parts (grouper in Python)
In this tutorial, we will learn how to split a string into equal-sized parts using Python. This technique is often called a "grouper" function and is useful for processing data in chunks. Problem Overview Given a string and a desired chunk size, we want to divide the string into equal parts. If the string length is not evenly divisible, we'll pad the last chunk with a fill character. Example 1 Split 'Tutorialspoint' into chunks of 5 characters ? string = 'Tutorialspoint' each_part_length = 5 print(f"Input: '{string}' with chunk size {each_part_length}") Input: ...
Read More