Category Python

rjust(): right-justified string in Python

how to python

Use the rjust() to right-justify a string. word = 'beach' number_spaces = 32 word_justified = word.rjust(number_spaces) print(word) #'beach' print(word_justified) #' beach' Notice the spaces in the second string. The word ‘beach’ has 5 characters, which gives us 27 spaces to…

Organizing Files in Python

how to python

This article covers operations you can do with a file regarding the Operating System: rename and move a file, check if a file exists, delete a file, make a copy of a file. This a direct extension of a previous…

While Loops in Python

how to python

Loops are used when you need to repeat a block of code a certain number of times or apply the same logic over each item in a collection. There are two types of loops: for and while. In this article,…

Type casting in Python

how to python

Explicit conversion To cast a variable to a string just use the str() function. my_str = str('32') # this is just a regular explicit intialization print(my_str) my_str = str(32) # int to str print(my_str) my_str = str(32.0) # float to…

Checking if a tuple is empty in Python

how to python

The pythonic way to check if a tuple is empty is using the not operator. my_tuple = () if not my_tuple: print("My tuple is empty") To check the opposite, if the tuple is not empty: my_tuple = ('amazon', 'microsoft') if…

Checking if a dictionary is empty in Python

how to python

The pythonic way to check if a dictionary is empty is using the not operator. my_dict = {} if not my_dict: print("My dictionary is empty!!!") To check the opposite, if the dict is not empty: my_dict = {1:'amazon', 2:'microsoft'} if…

Checking if a string is empty in Python

how to python

The pythonic way to check if a string is empty is using the not operator. my_string = '' if not my_string: print("My string is empty!!!") To check the opposite, if the string is not empty: my_string = 'amazon, microsoft' if…

Checking if a list is empty in Python

how to python

The pythonic way to check if a list is empty is using the not operator. my_list = [] if not my_list: print("My list is empty!!!") To check the opposite, if the list is not empty: my_list = ['amazon', 'microsoft'] if…