Skip to content

Instantly share code, notes, and snippets.

@AnisahTiaraPratiwi
Created March 23, 2021 06:46
Show Gist options
  • Save AnisahTiaraPratiwi/fc6357f0544fe480770e751502b379dc to your computer and use it in GitHub Desktop.
Save AnisahTiaraPratiwi/fc6357f0544fe480770e751502b379dc to your computer and use it in GitHub Desktop.
Question 1 Given a list of filenames, we want to rename all the files with extension hpp to the extension h. To do this, we would like to generate a new list called newfilenames, consisting of the new filenames. Fill in the blanks in the code using any of the methods you’ve learned thus far, like a for loop or a list comprehension.
filenames = ["program.c", "stdio.hpp", "sample.hpp", "a.out", "math.hpp", "hpp.out"]
# Generate newfilenames as a list containing the new filenames
# using as many lines of code as your chosen method requires.
newfilenames= [file.replace('.hpp','.h') for file in filenames]
print(newfilenames)
# Should be ["program.c", "stdio.h", "sample.h", "a.out", "math.h", "hpp.out"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment