python padding formatting alternative? [duplicate] - python

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"

Related

Python: Printing a function as a string inside of another printed string? [duplicate]

This question already has answers here:
What is the purpose of the return statement? How is it different from printing?
(15 answers)
Closed 1 year ago.
I am very new at python. I am wondering of the easiest way or alternative to defining a function as a string, and then printing said string in another print line, such as demonstrated here:
def world():
print("World!")
print("Hello " + world())
I am sure that this is an easy fix/search, but I cannot seem to find what I am searching for. Thank you!
I'm not sure what you are trying to accomplish here. But the answer you are looking for is,
def world():
return ("World!")
print("Hello " + world())

What is the difference between the f" and the format() method in python [duplicate]

This question already has answers here:
f-strings vs str.format()
(5 answers)
Closed 2 years ago.
So i've tried the formats string methods in python but what are the differences between them? Which method is best for me?
example1:
name = 'Dash'
print(f'Hello {name}!')
example2:
name = 'Dash'
print('Hello {}!'.format(name))
Effectively both do the same thing.
The f you mention is an f-string which is available from python 3.6
Print f is just a newer and easier way of inserting a variable into a string. I think it came in in python 3.6 . Both do really the same thing

How to use ' in sentences without having it think its closing? [duplicate]

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".

How to change 000-000 to 000000 in Python? [duplicate]

This question already has answers here:
How to delete a character from a string using Python
(17 answers)
Closed 4 years ago.
I'm making an odoo10 module, but I need to change a string like
000-000
to
000000
How can I do that?
print("000-000".replace("-", "")) #Output: "000000"
Try this
Oldstr = “000-000”
Newstr = Oldstr.replace(“-”,””)
That should do it.
What I did:
Make the string “000-000”
Made a new string and replaced the “- ”in the old one with nothing and saved it in a new variable

pythonic way to write the code for search and replace string [duplicate]

This question already has answers here:
Search and replace operation
(2 answers)
Closed 6 years ago.
I have written code to search and replace string in make command as per user input
make language='english' id=234 version=V1 path='/bin'
In above code i searched version=V1 and replace version with version=V2
import re
strings = "make language='english' id=234 version=V1 path='/bin'"
search_pattern= re.search('version=(.*?)\s', strings)
old_str = search_pattern.group(1)
print test.replace(old_str, "V2")
Can anyone help me write above code in pythonic way or any other way to write above code
It's very easy if you use str.replace
String = "make language='english' id=234 version=V1 path='/bin'"
String = String.replace("version=V1", "version=V2")

Categories

Resources