Line print in python [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am using Python 3 and Pycharm 2021.3. I want to print "4" on the first line. And "3" on the second line. I wrote code like this:
a="4"
b="3"
print(a\n,b)
It show a Error message. Please tell me the right way to write this. Thanks in Advance.

print(a + "\n" + b)
Or
print(a)
print(b)
Or
print(a, b, sep="\n")

When you want to explicitly add a new line, you should use "\n", like you did. However, you mustn't forget that this is a character, so you need to surround it by " or '.
Next, when you want to concatenate string variables, the standard is the use of +.
Put together, if you want to concatenate three variables, in which one of them is a string, a possible way would be:
final = a + '\n' + b
print(final)

Related

Python question: My function isn't running, using the input values I have set. Python 3.9 [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 months ago.
Improve this question
I am trying to create a function to convert DMS to DD. My function isn't running utilizing the import answer I provide. I an new to python, any assistance would be great.
import math
def d_m_s(d,m,s):
d = int(d)
m = int(m)
s = int(s)
dd = d + m/60 + s/3600
print(dd)
d,m,s = input("Enter degrees, minutes, and seconds:").split(',')
print(type(d))
You have called the type() function in your print statement. This will just tell you if d is a string, int, float, etc. In order to print "d", just use print(d).
However, in order to print the DD conversion, instead of ending with a print statement, you can replace the last line with d_m_s(d,m,s)
Hope this helps!

Reverse a string with users input [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I decided to take on some programming projects and decided to follow this guide:
https://www.dreamincode.net/forums/topic/78802-martyr2s-mega-project-ideas-list/
I started with the first project, which is to reverse a string, pretty simple stuff as you can find out how to do that in the documentation. IE.
txt = "Hello World"[::-1]
print(txt)
dlroW olleH
s = "health" [::-1]
print(s)
htlaeh
And So on... There's two questions really. In this guide i'm not to sure if they just wanted you to reverse the string. It seems like they want Input? For the user to Enter the String.
In this case i thought it might be something like this:
string = Input("Enter a string")
print(s [::-1])
How ever this did not work, Not to sure how to implement this in Python 3.8.2. Could someone let me know and how they did it that would be great.
Thank you.
Your code has a space between s and the indices you want. You also have two variable names, s and string. Choose one. Finally, Input must be lowercase. Change it to this:
string = input("Enter a string")
print(string[::-1])
Your print statement needs some modifications.
Here it goes-
print(string[: :-1])

How to replace a ' from a String in python [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I am trying to remove a " ' " character from a python string .
Below code gives a syntax error, How to achieve this task
final = string.replace(old_str, '\'', '')
To replace single quotes
final = old_str.replace("'", "")
str = "this is string example....wow!!! this is really string"
print(str.replace("is", "was"))
Here's an example of how to correctly use the replace function. This replaces all the is's with was's. It's hard to see what you're trying to do but use this as a guide.
You can do this
string = "some ' string"
final = string.replace('\'','')
print(final)
output
some string

Python-printing the wrong way [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I have entered the following code:
print("The new value for x is: ",x)
and the value of x is 9.
I expected that it will display something like: The new value for x is:9
but it displayed:
('The new value for x is: ', 9)
How can I fix that? also, my python version is:2.7.13
In python 2.7 you do not use braces when calling print.
The correct syntax for printing a string with a parameter concatenated to it in python 2.7 would be:
print "The new value for x is: " + str(x)
The syntax you are using is the python 3 syntax.
Note
As was stated in the comments by #ArpitSolanki, the code you used actually creates a tuple in python 2.7, and prints the result, hence you see the braces in the output.

New line (/n) not working in Python 2 [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I tried to make a power calculator program in Python 2.7. It worked but I tried to write the values into a file and the /n didn't work. Here is the program:
import math
file = open("numbers.txt" , 'w')
c = 0
a = int(raw_input("A number: "))
b = int(raw_input("To the power "))
h = range(b)
h.append(b)
print 1
file.write('1')
for c in range(b):
print int((math.pow(a, h[c+1])))
k = (int((math.pow(a, h[c+1]))))
file.write((str(k)+"/n") `
You're using a normal slash (/). But you need to use a Backslash (\).
So, \n will add a new line.
\ is used for escape sequence.

Categories

Resources