Python
26 NovemberRemove Empty Lines from Text File in Python
Use isspace() Method To remove empty lines from a Text File in Python: Open a file in read and write mode(r+). Use for loop for iterate over each line in the file. Use isspace() method to check if line is empty. if it is not empty, add it to result. Use seek(0) to move cursor […]
25 NovemberCheck if Variable Is None in Python
In Python, Check if Variable Is None and Check if Variable Is null have same solutions as meaning of both queries is same. 1. Introduction In Python Programming, checking when a variable is None(Python equivalent of null or nil in other languages) is a common task, particularly in functions returning no value, handling optional parameters […]
25 NovemberPrint Elements of List on Separate Lines in Python
Lists are one of the fundamental data types in Python that is capable of storing several items in one single variable. Elements of the list are all generally printed in a single line. However, there is often a need to print all the elements on separate lines, which is what this article will be focused […]
25 NovemberPrint String Till Character in Python
A string can be termed as a collection of characters with each character occupying a particular position. Strings cannot be altered and are considered immutable in Python. Print String Till Character in Python This tutorial will demonstrate how to print string till character in Python. Using the for loop and break statement A string can […]
25 NovemberRemove NewLine from String in Python
1. Introduction Removing newline characters from strings is a common task in Python, especially in text processing and data cleaning. However, newline characters can vary across different operating systems: Unix-like systems (including Linux and macOS) use \n (Line Feed), while Windows uses \r\n (Carriage Return and Line Feed). Therefore, a solution that works uniformly across […]
24 NovemberPrint Methods of Object in Python
💡 TL;DR To get all methods of object, use inbuilt dir() method str = "java2blog" ## Print methods and attributes of object result = dir(str) print(result) ## Print only methods of objects methodList = [attribute for attribute in dir(str) if callable(getattr(str, attribute)) and attribute.startswith('__') is False] print(methodList) Working with Python methods and attributes is very […]
24 NovemberPrint Variable and String in Same Line in Python
💡 TL;DR To Print Variable and String in Same Line in Python, use print() method and separate String and variable with comma. a = 2 print("Java", a, "Blog") # Output: Java2Blog Print Variable and String in Same Line in Python We use the print() function in Python to display output. It was changed to a […]
20 NovemberExit if Statement in Python
Using break Statement Use the break statement to exit if statement in Python. for num in range(6): print(num) if num == 3: break print('Loop Exited') 0 1 2 3 Loop Exited The break statement is used to terminate the loop prematurely. This statement is commonly utilized in loops to exit from them if a particular […]
18 NovemberHow to Remove Decimals in Python
Quick Solution To Remove decimals in Python, use int() function. a =3.7 print(int(a)) Or use math.trunc() import math a=3.7 print(math.trunc(a)) Output 3 Please note that when we remove decimal places, number before decimal point(.) will remain the same. If you want to round it up or down, then you should use round() instead. Two of […]