Looks like you want repr()
>>> """skip
... line"""
'skip\nline'
>>>
>>> print(repr("""skip
... line"""))
'skip\nline'
>>> print(repr("skip line"))
'skip\tline
So, your function could be
print_all = lambda s: print(repr(s))
And for Python 2, you need from __future__ import print_function
Looks like you want repr()
>>> """skip
... line"""
'skip\nline'
>>>
>>> print(repr("""skip
... line"""))
'skip\nline'
>>> print(repr("skip line"))
'skip\tline
So, your function could be
print_all = lambda s: print(repr(s))
And for Python 2, you need from __future__ import print_function
Even easier, cast it to a raw string by using "%r", raw strings treat backslashes as literal characters:
print("%r" % """skip
line""")
skip\nline
Additionally, use !r in a format call:
print("{0!r}".format("""skip
line"""))
for similar results.
Python: How can you print strings one character at a time?
Printing a range of characters after a specific character in a string
Python: print specific character from string - Stack Overflow
Remove all "non-printable" characters.
There's probably a more efficient way, but you could loop over the string character by character and build up a new copy composed of only the characters which are in string.printable.
More on reddit.comVideos
SO, my code is
filename = input("File name: ")
if filename.endswith (".html"):
print("This is an HTML file.")
else:
print("This is a", filename[7:11], "file.")If I input "whateva.jpg", it returns "This is a .jpg file.", because it prints the 7th to 11th characters from the input. But this only works when the input is a specific length. What I want is to be able to input a file name of any length, followed by any file type, and have it print "This is a .whateverTheFileTypeIs file."
Is there a way to have it split the input from whatever character comes before the period in the file type, and print from the period onwards? So if I input abcd.pdf, it'll return "This is a .pdf file", or if I input abcdefghijklmnop.docx, it'll print "This is a .docx file."?
I'm new so sorry if i'm not articulating this too well. Any help will be much appreciated
print(yourstring[characterposition])
Example
print("foobar"[3])
prints the letter b
EDIT:
mystring = "hello world"
lookingfor = "l"
for c in range(0, len(mystring)):
if mystring[c] == lookingfor:
print(str(c) + " " + mystring[c]);
Outputs:
2 l
3 l
9 l
And more along the lines of hangman:
mystring = "hello world"
lookingfor = "l"
for c in range(0, len(mystring)):
if mystring[c] == lookingfor:
print(mystring[c], end="")
elif mystring[c] == " ":
print(" ", end="")
else:
print("-", end="")
produces
--ll- ---l-
all you need to do is add brackets with the char number to the end of the name of the string you want to print, i.e.
text="hello"
print(text[0])
print(text[2])
print(text[1])
returns:
h
l
e