Adding text to function and separating values - python

Can anyone tell me how I can add text to my output? I tried to do print('The cpt/stuff pets folder contains the following:' +(os.listdir('/Users/raelynsade/Documents/cpt180stuff/pets')) and I got an error so I'm not sure how to go about.
Also can anyone tell me how I can separate the two values on separate lines, its joining them together
print(os.path.getsize(os.path.join('/Users/raelynsade/Documents/cpt180stuff/pets/dogs', 'dognames.txt')), end='') print(os.path.getsize(os.path.join('/Users/raelynsade/Documents/cpt180stuff/pets/dogs', 'dogs.jpg')), end='')
print(os.listdir('/Users/raelynsade/Documents/cpt180stuff'))
print(os.listdir('/Users/raelynsade/Documents/cpt180stuff/cars'))
print(os.listdir('/Users/raelynsade/Documents/cpt180stuff/pets'))
print('The Documents/cpt180stuff/pets/cats folder contains the following:')
print(os.path.getsize(os.path.join('/Users/raelynsade/Documents/cpt180stuff/pets/cats', 'catnames.txt')), end='')
print(os.path.getsize(os.path.join('/Users/raelynsade/Documents/cpt180stuff/pets/cats', 'cats.jpg')), end='')
print('The Documents/cpt180stuff/pets/dogs folder contains the following:')
print(os.path.getsize(os.path.join('/Users/raelynsade/Documents/cpt180stuff/pets/dogs', 'dognames.txt')), end='')
print(os.path.getsize(os.path.join('/Users/raelynsade/Documents/cpt180stuff/pets/dogs', 'dogs.jpg')), end='')

No. 1 You can use str.format() for that:
print(f"The cpt/stuff pets folder contains the following: {os.listdir('/Users/raelynsade/Documents/cpt180stuff/pets')}")
No. 2 don't use print(end=""). If you want a newline every print, just use print(value). end="" removes the newline. Refer this

Related

Printing over 50 emoji's in one line forces unwanted whitespace (Python) (Visual Studio Code)

Currently working on making a python boardgame representation in the terminal using emoji's, but as soon as my emoji print gets past 50 emoji's, it forces whitespace. This does not happen when printing a string of 50 normal characters or a string of equally lengthed characters. When I do even more emoji's, things sometimes get even weirder with question mark emoji's (see
Terminal picture).
s = "\U0001F7E8"
print("50:")
print(s*50)
print("\n51:")
print(s*51)
print("\n60:")
print(s*60)
print("\n70:")
print(s*70)
print("\n80:")
print(s*80)
print("\n220:")
print(s*220)
print("what I'm currently working on")
#I made string_representation with a lot of code above which I don't feel like matters for my question, so I left that out.
print(string_representation)
print("sadsfdgfhgjgfdssadsfdgfhgjgfdssadsfdgfhgjgfdssadsfdgfhgjgfdssadsfdgfhgjgfdssadsfdgfhgjgfdssadsfdgfhgjgfdssadsfdgfhgjgfds")
I tried locally, it's the problem with the python multiple print(*) and the emoji you take('\U0001F7E8'). You'd better switch to some other emojis.
# s = '\U0001F7E8'
s = '🐍'
# s = '🟨'
s5 = s*5
print(s*60)
for i in range(60):
print(s, end="")
print()
for x in range(6):
print(s*10, end="")
print()
print(s5*12)
print()
for i in range(12):
print(s5, end="")
print()
for x in range(4):
print(s5*3, end="")
print()
Try to print them in one print condition like this
print("50:", s*50, "\n51", s*51, "\n51")
and so on maybe thats your problem
because in the string representation you added them in one print condition

Python \n gives two spaces in between output and \t gives one

I do not understand why when inputing a space between the code with \t gives one space of line between the 'green' and 'Some things I learned so far:' output. When I use \n it gives two spaces inbetween. Shouldn't the space be the same for either \t and \n? I know that \t does tab and \n is new line. but I do not understand how \n does two spaces inbetween
Code is:
fav_num = {
'rachel':'blue',
'hannah':'green',
}
print(fav_num['rachel'])
print(fav_num['hannah'])
#6-3
coding_glossary = {
'list':'mutable type where you can store info',
'tuple':'immutable type similar to list',
'string':'simple line of code'
}
print('\t')
print('Some things I learned so far: \n')
print('What a list is:')
print(coding_glossary['list'])
Output is :
blue
green
Some things I learned so far:
What a list is:
mutable type where you can store info
Process finished with exit code 0
python's built-in print function has '\n' as end character implicitly.
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False):
Print objects to the text stream file, separated by sep and followed by end. sep, end, file and flush, if present, must be given as keyword arguments
So, every time you run print() there is a '\n' character that gets printed implicitly unless you override the behavior by passing end= to it. (like end='' for instance)
Your code can be equivalently written:
#
print()
print(β€˜Some things I learned so far:’)
print()
#
print by default goes to the next line. try
print(" ",end = "")
so you can see '\t' more clearly.
Also, tab jumps to the next block. A block is usually 4 spaces.
Try this and notice where the . is:
print("\t", end=".\n")
print("A\t", end=".\n")
print("ABC\t", end=".\n")
print("ABCD\t", end=".\n")
The statement print('\t') is printing a tab, then returning to the next line, as the default print function automatically adds a newline. So you can't see the tab, but it is there. When you add \n to the end of the string you print, it adds a line return in addition to the default line return.
To remove the default line return, specify the 'end' parameter of the print function:
print('abcd\n', end='')
This will only include one line return.
By default print put a new line at the end, to modify this behavior you can set the end parameter with end=""
Example:
print("this will use 2 lines \n")
print("this will use 1 line")
print("this will use 1 line \n", end="")
Since print() does give an '\n' string at the end of each output the command print('\n') gives the commandline string '\t\n'.
For more details please see the following well documented post Link
"\n" character is a newline character and when you print "\n" it sets the cursor to a new line. print always sets a new line in the end by default. But you can change that behavior by setting the value of end argument to an empty string.
print("hello", end="")
"\t" is a tab character
for i in range(20):
print("current number is\t", I)
# current number is 0
# current number is 1
# current number is 2
# current number is 3
# current number is 4
# current number is 5
# current number is 6
# current number is 7
# current number is 8
# current number is 9
# current number is 10
# current number is 11
# current number is 12
# current number is 13
# current number is 14
# current number is 15
# current number is 16
# current number is 17
# current number is 18
# current number is 19
Find more magic characters which can be useful in your programs

How do I print a sentence for an assigned output/input?

I have a code that allows a user to choose between 3 options
Ie; Beginner: 1, Intermediate: 2 and Advanced: 3.
However, I'm wanting to generate a paragraph that's assigned to each of the 3 options.
For example
If the user has entered 1 for Beginner, the output will follow with "Hi Beginner! We are going to learn about ....."
The code I've tried thus far is just using the print(" ")option followed by
if f==0:
print("You have entered " + str(inp) + ": " + out).
However, as I'm writing a long paragraph, the output is messy.
Well, i think that you're printing a line without using "\n" in your print text, that's why the output will be always a entire line.
You can use "\n" to write different lines:
print("This is a line\nNow this is other")
Or you can use triple quotes:
print("""This is a line
Now this is other""")

How would I create a new line in Python?

I am using Python 2.7 and this is what I am working with
print( "Massa: ", line.count("massa"))
# trying to create a new line between these two lines
print( "Lorem: ",line.count("lorem")+1)
I tried this
print( "Massa: ", line.count("massa"))\n( "Lorem: ", line.count("lorem")+1)
and did not get the results I was looking for
If you mean that you want to print it with a single print statement, this will do it.
print "Massa: ", line.count("massa"), "\n", "Lorem: ", line.count("lorem")+1
Since you are using Python 2.7 I removed the enclosing brackets, otherwise the strings are treated as elements of a tuple. I have also added a new line character \n in the middle to separate the output into 2 lines.
If you print the itmes as 2 separate print statements a new line will appear between them:
print "Massa: ", line.count("massa")
print "Lorem: ", line.count("lorem")+1
I think you can just use:
print ""
to print a newline
\n creates a line break. You can pass it as a single string and use format to place your parameters.
print("Massa: {0}\nLorem: {1}".format(line.count("massa"), line.count("lorem")+1))

Why is print() not working anymore with python?

in fact this should not be a problem because it's fairly basic. I just want to print out an array of categories, but in one line and separated by comma.
for entry in categories:
print(entry, ", ", end='')
Until now it worked perfectly but now it doesn't print anything. If I delete the last part ", end=''" it works but then I can't get everything in one row without a new line for each category.
Can someone explain to me why it isn't working any more?
You are almost certainly experiencing output buffering by line. The output buffer is flushed every time you complete a line, but by suppressing the newline you never fill the buffer far enough to force a flush.
You can force a flush using flush=True (Python 3.3 and up) or by calling the flush() method on sys.stdout:
for entry in categories:
print(entry, ", ", end='', flush=True)
You could simplify that a little, make , the end value:
for entry in categories:
print(entry, end=', ', flush=True)
to eliminate the space between the entry and the comma.
Alternatively, print the categories as one string by using the comma as the sep separator argument:
print(*categories, sep=', ')
Check categories isn't empty - that'd make it print nothing - also I'd consider changing your code to make use of the sep argument instead (depending how large categories is):
print(*categories, sep=', ')
eg:
>>> categories = range(10)
>>> print(*categories, sep=', ')
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Then you don't have to worry about flushing/trailing separators etc...
Printing to the terminal in Python 3 is usually "line buffered". That is, the whole line is only printed to the terminal once a newline character is encountered.
To resolve this problem you should either print a new line at the end of the for loop, or flush stdout.
eg.
for entry in categories:
print(entry, ", ", end='')
print(end='', flush=True) # or just print() if you're not fussed about a newline
However, there are better ways of printing an array out. eg.
print(", ".join(str(entry) for entry in categories))
# or
print(*categories, sep=", ")

Categories