I would like my code to look beautiful
Thanks in advance..
views:
744answers:
11s = 'a.pdf'
print s.replace('pdf', 'jpg')
That what you're looking for?
i don't know if it's elegant:
string.replace
:>>> s = "a.pdf" >>> s.replace(".pdf", ".jpg")
'a.jpg'
regular expression:
>>> import re >>> re.sub("\.pdf$", ".jpg", "a.pdf") 'a.jpg'
os.path
>>> import os.path >>> name, ext = os.path.splitext("a.pdf") >>> "%s.jpg" % (name) 'a.jpg'
string index:
>>> s = "a.pdf" >>> s[-3:] == "pdf" and s[:-3] + "jpg" 'a.jpg'
The most "correct" way of doing this would be something like the following. This would handle it no matter what the extension is, regardless of platform, and generally without reinventing the wheel.
import os.path
root, ext = os.path.splitext('a.pdf')
# Optional error checking, if necessary:
if ext.lower() != '.pdf':
raise ValueError('File lacks proper extension')
filename = "%s.jpg" % (root,)
Not the most elegant perhaps, but safer:
root, ext = os.path.splitext(s)
if ext != '.pdf':
# Error handling
else:
s = root + '.jpg'
Use the $
from regex to make sure you replace the file extension.
>>> import re
>>> s = 'my_pdf_file.pdf'
>>> re.sub('\.pdf$', '.jpg', s)
'my_pdf_file.jpg'
I's suggest 1ch1g0's solution, but instead of s[-3:]=='pdf'
: s.endswith('.pdf')
and no '+' for strings which is slow:
>>> s = 'a.pdf'
>>> s.endswith('.pdf') and ''.join([s[:-3], 'jpg'])
'a.jpg'
Easy:
string s="a.pdf";
s="a.jpg";
I am afraid your question leaves much to be desired...
Strings are immutable in Python, so basically you cannot alter it.
If you want a new string, there are plenty of options, depending on what you exactly want.
def silly_thing(s):
return s[:-4]+".jpg" if s[-4:] == ".pdf" else s
This one requires NumPy, but of course that means it will run much faster than the alternatives:
>>> s = 'a.pdf'
>>> from numpy import array
>>> ''.join([chr(c) for c in (array([ord(c) for c in s]) + [0, 0, -6, 12, 1])])
'a.jpg'
Of course, whether you consider this "elegant" or not depends on your definition of "elegant" but, as with all other useful information, the question didn't include that...
Edit: yes, this is a joke, but trying to make a point...
With some inspiration from @Peter Hansen's answer I managed to create a function that accomplishes pretty much what you need. While his approach is nice, it is a bit lacking in that it can only convert file names that are exactly 5 characters long.
My solution fixes that:
from numpy import array
def convert_pdf_filename_to_jpg_filename_in_a_really_really_elegant_way(s):
"""
Examples:
>>> convert_pdf_filename_to_jpg_filename_in_a_really_really_elegant_way("a.pdf")
'a.jpg'
>>> convert_pdf_filename_to_jpg_filename_in_a_really_really_elegant_way("myfile.pdf")
'myfile.jpg'
"""
return ''.join([chr(c) for c in (array([ord(c) for c in s]) + list([0] * (len(s) - 3) + [-6, 12, 1]))])
I am quite satisfied with this code. I would not mind if it was added to the Python standard library (perhaps in a really_really_elegant_code
module?). But than that would require that numpy was added to the standard library as well. Does anyone have an idea if that is likely to happen?