This question already has answers here:
How to print a list of tuples with no brackets in Python
(5 answers)
Closed 2 years ago.
First post!
I am doing a basic python program, this stuff is very advanced for me :D, and I want to strip off the characters ' , () from a tuple when it is printed. What I have, that prints the list out without stripping, is:
view = map(str, listplanets)
print("\n".join(view))
"listplanets" is the name for the tuple, though you guys probably know this XD. I tried view = map(str, listplanets).strip("\"',") and I have tried moving this strip command to every place I could think of. I always get an error saying that the map does not have the attribute strip. If I convert the tuple into a string like so view(str(listplanets)) it will print out each character on a separate line rather than each tuple item. This is the output that I get:
('Mercury', 0.378)
('Venus', 0.907)
('Mars', 0.377)
('Io', 0.1835)
('Europa', 0.1335)
('Ganymede', 0.1448)
('Callisto', 0.1264)
It would be greatly appreciated if someone can answer this for me :).
lines = ['{} {}'.format(planet, n) for planet, n in listplanets]
print('\n'.join(lines))
Related
This question already has answers here:
How to calculate an equation in a string, python
(2 answers)
Closed 2 years ago.
I'm using Python 3.7.4. I was wondering about how to keep track of symbols like: +, /, -, * in a string. But I mean with out the '' and "" in front and behind of it. I'm creating a calculator as my project. This is what it looks like:
When ever you click on one of the buttons it adds that number to a string like user_text = ''. So like a blank string. So say you have in 9 + 9 the string when ever you added it together you get 18. But the problem lies with: +, /, -, *. Cause I know how to turn a string into a number and then add them together or any other way. But, how would you keep track of the symbols in the string and add the numbers in the string to each other with the symbol with it. So, with the correct operation.
I've tried to do: if '+' in len(user_text): print("Yes") but then I realize that it can't iterate a string for int. Anything with range is out of the question I realized too. I was thinking about having like a back up line, but as a list then append what ever was entered onto the list. Then keep track of it. Like say user_list = [] then you added 4 + 4 onto the list user_list = ['4', '+', '4']. But then again how would I keep track of the symbols I said, but then add the 2 strings numbers together as an int to get 8. I just can't think of a way to do something like this. I might be overthinking this but I just can't think of it.
If I can provide anymore information on my issue or anything, let me know. I appreciate the advice and help. Thank you.
Have you considered using python's eval()? Since your calculator probably doesn't use the same operator symbols as python you might have to tweak the resulting string from your calculator to make it work, but it sounds like eval() should do the job for you.
This question already has answers here:
member variable string gets treated as Tuple in Python
(3 answers)
Closed 4 years ago.
I am new to python and trying to experiment something with strings.
I have below commands.
s7="hello",'world'
print(s7)
Output was : ('hello', 'world')
As per my understanding, it should not print angular braces and it should not print hello in single quotes. Could you help me in understanding this?
Those are not angular braces. They are parentheses, and they indicate that what you are printing is a tuple. A tuple is kind of sequence, in this case a pair of strings.
Your first string is "hello", and your second string is 'world'.
You could have expressed them as 'hello' and "world" and produced exactly the same strings. The quote marks (either kind) are there to indicate the content of the string, but they are not part of the content of the string.
You assigned not 1, but 2 strings (in a tuple) to s7, which explains how it was printed.
s7 = "\"hello\",'world'"
seems like it would assign the string you want. (Note the use of \"to indicate a " inside of a string surrounded by "s, as #NielsHenkens points out.)
This question already has answers here:
Understanding slicing
(38 answers)
Closed 5 years ago.
Say I have x = 'abcde'. If I write x[::] I get 'abcde', If I write x[::2] I get 'ace'. So the spaces between the colons implies for the first space, "the start of the list" and for the second space "the end of the list". It's as if you were writing x[0:len(x)] right?
Okay, so when I write x[::-1] I get the list completely in reverse, i.e. 'edcba'. So what are the equivalent values I could substitute in?
x[len(x)-1:0:-1] won't work because the second space is not including that number, so I would get 'edcb' and if I went x[len(x)-1:-1:-1] it would simply do nothing because x[len(x)-1] == x[-1].
Yes, it will always work if I leave the colons in, my confusion comes is what does just having x[::] actually doing, what values is it actually substituting? Because if it were always just "the start of the list and the end of the list" then there's no way it would work for x[::-1], it would simply print nothing, and if it somehow knows to reverse it, then what values is it putting because even if we didn't have the problem of not being able to terminate at the right place (i.e. can't put 0 in the middle space because that wouldn't be include but can't put -1 because that's the end of the list) we have the problem that it should still start at 0, i.e. we should have
aedcb instead?
Can anyone shed light on the behind the scenes magic here?
Thank-you.
(Yes I googled for this, and tried to look it up in the docs and could find no explanation to this specific question)
edit: The answer to my question can be found in the non accepted answer of Understanding Python's slice notation, namely, https://stackoverflow.com/a/24713353/4794023.
Thanks guys
Slicing will include the element at start index, and will not include the stop index. Since a "stop" index of -1 already has a different meaning (used to wrap around to the end of the string) then you'll have to use a None instead:
>>> x[len(x)-1:None:-1]
'edcba'
This question already has answers here:
How do I escape curly-brace ({}) characters in a string while using .format (or an f-string)?
(23 answers)
Closed 6 years ago.
again :)
I found this bit of code
col_width=[13,11]
header=['First Name','Last Name']
format_specs = ["{{:{}}}".format(col_width[i]) for i in range(len(col_width))]
lheader=[format_specs[i].format(self.__header[i]) for i in range(nb_columns)]
How Python evaluate this statement? Why we use three { when we have one element to format in every iteration?
when you do {{}}, python skips the replacement of {} and makes it the part of string. Below is the sample example to explain this:
>>> '{{}}'.format(3) # with two '{{}}'
'{}' # nothing added to the string, instead made inner `{}` as the part of string
>>> '{{{}}}'.format(3) # with three '{{{}}}'
'{3}' # replaced third one with the number
Similarly, your expression is evaluating as:
>>> '{{:{}}}'.format(3)
'{:3}' # during creation of "format_specs"
For details, refer: Format String Syntax document.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Reverse a string in Python
Its been stumping me despite my initial thoughts that it would be simple.
Originally, I thought I would have have it print the elements of the string backwards by using slicing to have it go from the last letter to the first.
But nothing I've tried works. The code is only a couple lines so I don't think I will post it. Its extraordinarily frustrating to do.
I can only use the " for ", "while", "If" functions. And I can use tuples. And indexing and slicing. But thats it. Can somebody help?
(I tried to get every letter in the string to be turned into a tuple, but it gave me an error. I was doing this to print the tuple backwards which just gives me the same problem as the first)
I do not know what the last letter of the word could be, so I have no way of giving an endpoint for it to count back from. Nor can I seem to specify that the first letter be last and all others go before it.
You can do:
>>> 'Hello'[::-1]
'olleH'
Sample
As Mike Christensen above wrote, you could easily do 'Hello'[::-1].
However, that's a Python-specific thing. What you could do in general if you're working in other languages, including languages that don't allow for negative list slicing, would be something more like:
def getbackwards(input_string):
output = ''
for x in range(0,len(input_string),-1):
output += input_string[x]
return output
You of course would not actually do this in Python, but just wanted to give you an example.