Found 10783 Articles for Python

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

267 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

318 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

3K+ 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.

string.octdigits in Python

Hafeezul Kareem
Updated on 11-Jul-2020 07:52:45

90 Views

In this tutorial, we are going to learn about the string.octdigits string.The string octdigits is pre-defined in the string module of the Python3. We can use the octal digits whenever we want in the program by simply accessing it from the string module.Example Live Demo# importing the string module import string # printing the octal digits string print(string.octdigits)OutputIf you run the above code, then you will get the following result.01234567The string.octdigits is a string. You can it by executing the following program.Example Live Demo# importing the string module import string # printing the octal digits string print(type(string.octdigits))OutputIf you run the above code, ... Read More

String slicing in Python to check if a can become empty by recursive deletion

Hafeezul Kareem
Updated on 11-Jul-2020 07:50:47

269 Views

In this tutorial, we are going to write a program that will check whether the given string can become empty or not by recursive deletion of chars using slice. Let's see an example to understand it more clearly.Inputstring = "tutorialstutorialspointpoint" sub_string = "tutorialspoint"OutputTrueAfter first iteration tutorialstutorialspointpoint becomes tutorialspoint.After the second iteration, the string will be empty.We can achieve the result using find() method of the string. Follow the below steps to write the program.Initialize the string and sub_string.If any of them is empty, then return FalseWhile string length is greater than zero. Do the following.Check whether the sub_string is present ... Read More

SQL using Python

Hafeezul Kareem
Updated on 11-Jul-2020 07:01:04

616 Views

In this tutorial, we are going to learn how to use SQL with Python using SQLite database. 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. Have a look at the steps and write the program.Import the sqlite3 module.Create a connection using the sqlite3.connect(db_name) the method that takes a database name is an argument. It creates one file if doesn't exist with the given name else it opens the file with the given name.Get the ... Read More

Split a string in equal parts (grouper in Python)

Hafeezul Kareem
Updated on 11-Jul-2020 06:59:37

278 Views

In this tutorial, we are going to write a program that splits the given string into equal parts. Let's see an example.Inputstring = 'Tutorialspoint' each_part_length = 5OutputTutor ialsp ointXInputstring = 'Tutorialspoint' each_part_length = 6OutputTutori alspoi ntXXXX We are going to use the zip_longest method from the itertools module to achieve the result.The method zip_longest takes iterators as argument. We can also pass the fillvalue for partitioning the string. It will return list of tuples that contains characters of equal number.The zip_longest return a tuple on each iteration until the longest iterator in the given in exhausted. And the tuple contains ... Read More

Sorted() function in Python

Hafeezul Kareem
Updated on 11-Jul-2020 06:58:18

273 Views

In this tutorial, we are going to learn about the sorted() function in Python.The function sorted() is used to sort an iterable in ascending or descending order. We can even sort the list of dictionaries based on different keys and values. Let's get most out of the sorted() function.The sorted() function is not an in-place algorithm like sort method.Default sorted()The function sorted() will sort an iterable in ascending order by default. Let's see an example.Example Live Demo# initializing a list numbers = [4, 3, 5, 1, 2] # sorting the numbers sorted_numbers = sorted(numbers) # printing the sorted_numbers print(sorted_numbers)OutputIf you run ... Read More

Advertisements