This question already has answers here:
Create a typewriter-effect animation for strings in Python
(7 answers)
Closed 8 years ago.
I'm somewhat new to programming and Python. I'm actually making a little function which write a sentence letter by letter. Here's the code :
import time
def slowWriting(txt, speed=0.01):
for c in txt:
print(c, end='\r')
time.sleep(speed)
print()
The issue is that when building the function with cxfreeze, and executing the .exe, for the line :
<module_name>.slowWriting("abcd")
Instead of displaying :
abcd
The console displays :
d
In fact, when a character is displayed, he replaces the previous displayed character...
How to make it work ?
Thanks for reading and your potential answer.
\r is carriage return. It returns the cursor to the beginning of the line. That is why you are seeing this behavior. You should use '' as the end character for print.
Related
This question already has answers here:
How to print without a newline or space
(26 answers)
Closed 2 years ago.
In class im meant to write a program that arranges coordinates for you. I wrote this:
x = input("")
y = input("")
z = input("")
print("(",x,",",y,",",z,")\n)
and the output is: (␣0␣,␣-7831␣,␣2323␣)⤶
how do I stop the extra spaces from appearing so I get this?: (0,␣-7831,␣2323)⤶
In modern Python, the nicest way is to use an f-string:
print(f"({x},{y},{z})")
Note how the string is prefixed with f. Everything between the curly braces {} then gets interpreted as a Python expression which is subsequently converted to a string and inserted at that point.
Note that print already follows up with a newline, so unless you want an extra one (that is, a blank line), you don't need to add \n yourself.
This question already has answers here:
Replace characters in string from dictionary mapping
(2 answers)
Closed 2 years ago.
Hey guys I am making an encoding system in which each letter gets converted into predefined gibberish.
For example, 'a' has already been set as 'ashgahsjahjs'.
But using if a in data: print("ashgahsjahjs") executes this for one time only, if there are more than one A in the word, it would not print them with gibberish.
Using a while loop does not work either as it keeps printing indefinitely, so is there a way to print the gibberish each time there is a new occurrence of a letter.
you could try indexing the string.
your_string = "are you an apple?"
for i in range(len(your_string)):
if "a" == your_string[i]:
print("Found a at position {pos}".format(pos=i))
else:
print("Nope")
This question already has answers here:
Why doesn't print output show up immediately in the terminal when there is no newline at the end?
(1 answer)
Python print immediately?
(1 answer)
Closed 2 years ago.
I have the following code:
for file_name, content in corpus.items():
print('here')
content = [list(filter(lambda index: index not in remove_indices, content))]
corpus[file_name] = np.array(content).astype(np.uint32)
Where corpus is a 800,000 long dictionary with string keys and array values.
Things were taking forever so I decided to check how fast each iteration was by adding in that print statement.
If I comment the last two lines out it prints lots of heres really fast, so there's no problem with my iterator. What's really weird is that when I uncomment the last two lines, here takes a long time to print, even for the first one! It's like the print statement is somehow aware of the lines that follow it.
I guess my question speaks for itself. I'm in Jupyter notebook, if that helps.
This question already has answers here:
Changing one character in a string
(15 answers)
Closed 2 years ago.
A question I came up with:
I'm trying to write a function that replaces the first and the fourth letter of a word with the same letter just capitalized.
Currently I am working with the string.replace() method. It works great for most of the time, except when there is an equal letter to the one on the fourth place before it.
Example: "bananas"
What I would expect the program to do is to return "BanAnas" but for a reason it return "BAnanas". If I use the word "submarine" it would just work fine, "SubMarine".
The code I wrote is this:
def old_macdonald(name):
name = name.replace(name[0], name[0].upper(), 1)
name = name.replace(name[3], name[3].upper(), 1)
return name
Can someone explain why this is happening?
It's because name.replace(name[3], name[3].upper(), 1) looks for the first character matching name[3]. Stop using replace altogether, chop up your string by slicing.
This question already has answers here:
Remove final character from string
(5 answers)
Closed 4 years ago.
I'm trying to delete the last character of a string, and every documentation I can find says that this works.
string = 'test'
string[:-1]
print(string)
However, whenever I try it, my IDE tells me that line two has no effect, and when I run the code it outputs "test" and not "tes", which is what I want it to do. I think that the documentation I'm reading is about python 2 and not 3, because I don't understand why else this simple code wouldn't work. Can someone show me how to remove the last letter of a string in python 3?
new_string = string[:-1]
print(new_string)
You must save the string in the memory. When we assign a variable to the string without the last character, the variable then "stores" the new value. Thus we can print it out.