Category: Python Tutorials

Python programming, installation, tutorials, errors, examples, how to guides for beginners

Python: Extract file name from URL path [with and without extension]

Python code to extract the file name from path or URL is explained below. Find the delimiter “/” position before the file name using   firstpos=path.rfind(“/”) Find the last position of the file name using lastpos=len(path) Extract the file name using path[firstpos+1:lastpos]. Add 1 to the first position to avoid extracting the delimeter “/”. Python code […]

Simple steps to install/uninstall pygame on windows [using pip]

Below are the simple steps to install pygame on Windows Operating system. Steps to install Pygame Install Python on Windows if you have not installed it Goto download pygame Goto Navigation  -> click Download files Download the latest version of pygame source file (.tar.gz file) Download the pygame package by clicking on pygame.x.x.x.tar.gz file link (x.x.x […]

Python code to get current year

If you are looking to get current year in integer format in Python programming language, then use below one from datetime import datecurrent_year = Source:

Strip trailing whitespaces from string [Python]


If you want to remove extra whitespaces from start and end of a string, use strip() method in Python. strip() method removes all default whitespaces (spaces and tabs \t) and returns the copy of the string. Python code to strip trailing whitespaces from string string = ” xyz is equal to xyz ” # string […]

Python: Position of next occurrence of a sub-string


Python code to get the position of next or second occurrence of a sub-string within a string. # Below code finds the position of second occurrence of “xyz” string = “xyz is equal to xyz” firstSubPos=string.find(“xyz”) # get the position of first substring “xyz” secondSubPos=string.find(“xyz”, firstSubPos+1) # get the position of second occurence of substring […]

Python: Replace/remove string between two delimiters/characters

This Python code helps you to to replace a string between two delimiters or characters delete/remove string between two characters or delimiters Python code to replace string between two delimiters # This macro replaces nivas in below string with silveri string = “sri[nivas]” firstDelPos=string.find(“[“) # get the position of [ secondDelPos=string.find(“]”) # get the position […]