This question already has answers here:
Python String Formatting - Type Error - Not enough arguments for format string
(2 answers)
Closed 3 years ago.
so for my problem, I have to create a program that takes a users input, and then prints it like a list. For example, if the user inputs "I like doing this activity, the program should return it as.
I
like
doing
this
activity.
But these should work with any scenario that the user inputs.
I tried using the input function and split to try and define the variables
my_list_of_data = input("Enter your sentence:").split()
print("(%s),/n(%s),/n(%s),/n(%s),/n(%s)"%(my_list_of_data))
I am getting an error in line 2, saying there is not enough arguments to create a string.
Print the list like this:
print('\n'.join(my_list_of_data))
Related
This question already has answers here:
Transform string to f-string
(5 answers)
Closed 2 years ago.
I'm trying to pull a string from JSON, then convert it to an f string to be used dynamically.
Example
Assigned from JSON I get
whose_fault= "{name} started this whole mess"
How to build a lambda to convert it to an f-string and insert the given variable? I just can't quite get my head around it.
I know similar questions have been asked, but no answer seems to quite work for this.
Better question. What's the most pythonic way to insert a variable into a string (which cannot be initially created as an f-string)?
My goal would be a lambda function if possible.
The point being to insert the same variable into whatever string is given where indicated said string.
There is no such thing as f-string type object in python. Its just a feature to allow you execute a code and format a string.
So if you have a variable x= 2020, then you can create another string that contains the variable x in it. Like
y = f"It is now {x+1}". Now y is a string, not a new object type,not a function
This question already has answers here:
String formatting in Python [duplicate]
(14 answers)
Putting a variable into a string (quote)
(4 answers)
Closed 2 years ago.
very new to python so sorry for the silly question
I've created a user input interface
fn=input()
#Where the user will input version32 for instance so effectively
fn=Version32
I've then imported a template word document using docx, which has been heavily modified based upon user input. I then want the file name output to be saved as "fn" or in this case Version32
output.save(r"C:\Users\XXX\XXX\XXX\'fn'.docx")
Where fn is a variable? Is this even possible, or am I barking up the wrong tree?
Kind Regards!!
IIUC, you can do this using f-string:
output.save(rf"C:\Users\XXX\XXX\XXX\{fn}.docx")
This question already has answers here:
How to get multiline input from the user [duplicate]
(5 answers)
Closed 3 years ago.
we face the problem while taking multi-line input for strings in python.
s=str(input("enter string"))
when i enter the input
for suppose let us assume that i entered my input as:
hello friends!!
good morning
when i try to print this i get only 'hello friends!!'
is there any other code to take until i press a special symbol to tell to compiler that it is EOF of my input
like in c we have code like scanf("%[^~]",str);
it takes the input until we press "~" symbol.
like this do we have any special operations in python.
expected output of above program is
hello friends!! good morning
This has been asked here many times. Please use the search function or click here: How to get multiline input from user
tldr:
import sys
msg = sys.stdin.readlines()
This question already has answers here:
Convert a list of characters into a string [duplicate]
(9 answers)
Closed 4 years ago.
I have written a code that ends up outputting what I want but in list format. Just to make it easier to understand, I will make up an input.
If I get
>>>
['H','e','l','l','o',' ','W','o','r','l','d']
as an output, how can I change it to:
>>>
'Hello World'
I have tried using .join() but it tells me that it does not work with lists as an error code.
If you need any more information, or I am being vague, just leave a comment saying so and I will update the question.
And if you leave a downvote, can you at least tell me why so that I can fix it or know what to improve for later posts
You join on the connector like this: ''.join(['H','e','l','l','o',' ','W','o','r','l','d'])
Just use join method by passing a list as parameter.
str = ''.join(['H','e','l','l','o',' ','W','o','r','l','d'])
This question already has answers here:
How to read a single character from the user?
(26 answers)
Python sys.stdin.read(1) in a while(True) loop consistently executes 1 time getting input and multiple times not getting input
(2 answers)
Closed 6 years ago.
I'm writing a Python program (on OS X/Linux) in which a user has to type some words in a while loop. This is the relevant part of it:
i = 0
array = []
while (i < 10):
array.append(raw_input("Prompt: "))
i = i + 1
However I'd like to stop the input as soon as the user types a SPACE, without the need to press ENTER.
I know I can't do this using raw_input, however I did not find any way to achieve what I'm trying to do. Basically I would like the user to be able to type one word after the other, using the SPACE button instead of ENTER to input the next word.
Do you have any idea? Thanks in advance.
It's not possible with raw_input, input or sys.stdin.read(1) either. There is no generic solution which works on every operating system.
Here is how to read a single character from the user in Python, without pressing Enter: Python read a single character from the user
Then you need to process each character in a for loop.