This question already has answers here:
Remove all whitespace in a string
(14 answers)
Closed 2 years ago.
I would like to see how I could remove a certain section of a string without leaving the blank space.
When running this code it outputs nice today, the problem is that I am replacing the characters with a blank space. Is there a fix to this or should I be using a completely different method entirely?
text = ("hello bye there ")
print(text.replace("hello", "nice").replace("there","today").replace("bye", ""))
you should remove white space at first:
text = ("hello bye there ")
print(text.replace("hello", "nice").replace("there","today").replace(" ", '').replace("bye" , " "))
the output will be:
nice today
Related
This question already has answers here:
How do I split a string into a list of words?
(9 answers)
Closed 2 years ago.
I have this String:
Hello World.\n
I'm very happy today.\n\n\n\n\n
How are you?\n\n\n
Bye.
And I want to split it by two or more new lines without using any libraries.
Output:
['Hello World.\n I'm very happy today','How are you?','Bye']
Python's base string function split() should work here, without the need to import anything:
inp = "Hello World.\nI'm very happy today.\n\n\n\n\nHow are you?\n\n\nBye."
terms = inp.split('\n\n')
print(terms)
This prints:
["Hello World.\nI'm very happy today.", '', '\nHow are you?', '\nBye.']
This question already has answers here:
How to fix syntax error when printing a string with an apostrophe in it? [closed]
(6 answers)
Closed 3 years ago.
bob = input('How old are you? ')
print('You're', bob)
It's giving me syntax error because im using ' for you're. Whats the correct way of handling sentences with ' in them?
There are at least two ways to do this:
Use " for your string: "You're".
Escape the single quote: 'You\'re".
This question already has answers here:
How can I fill out a Python string with spaces?
(14 answers)
Closed 5 years ago.
Is there a custom way of padding lines of text in python, I am using the escape characters "\t", but I wonder if there is an alternative.
for example
print('My Name is:')
print('Rambo')
print('Mambo')
Output:
.My Name is:
.....Rambo
..Mambo
Try using:
print('{:>15}'.format('My Name is:'))
Refer for examples:
PyFormat
Write a simple function for yourself.
def p(a,b):
print(" "*a + b)
p(1,"while")
This should return:" while"
This question already has answers here:
How to print without a newline or space
(26 answers)
Closed 6 years ago.
I tried to get the output of python as
def main():
i=raw_input()
a=input()
print(i*a)
main()
input:hello,
2.
output: "hellohello"
but i am getting output as
output:" hello
hello "
.how can i get the two strings as output continuously in same line from one print statement.for example:"hellohello "
To remove the white space from the single execution of print. Use .strip() function as (as I believe you have white space within your i string):
print(i.strip()*a)
But if you want all the executions of print in single line, you should use end argument of print to define empty string. Your print statement should be like:
print(i.strip()*a, end='')
based on the fact that you are using raw_input I'm assuming you are using python 2. Therefor you could just change the print() with a print and your code should work
def main():
i=raw_input()
a=input()
print i*a <---
main()
This question already has answers here:
How to change a string into uppercase?
(8 answers)
Closed 9 years ago.
I created a .txt file with random words that I'm importing into my script. I want then to take all the words, print one on each line and convert them all to uppercase. I have the first part done:
a=open("redchief.txt").read().split()
print ' \n'.join(a)
I'm having problems converting the data into capital letters. Here is some of the data:
It looked like a good thing: but wait till I tell you.
Just change your last line from:
print ' \n'.join(a)
to:
print ' \n'.join(a).upper()
you don't have to store the result in a separate variable first, since ' \n'.join(a) gives you a string object whose upper() method you can call.