Using python string format with placeholder [duplicate] - python

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 2 years ago.
I know how to use string format in python
"name = {fname}".format(fname = "John")
The output is name = John
But how can I use if I need to input {} inside the string that not belong to variable , for example
"{NOTVAR} name = {fname}".format(fname = "John")
I want to output will be {NOTVAR} name = John

print("{{NOTVAR}} name = {fname}".format(fname="John"))
Output
{NOTVAR} name = John

Try
"{{NOTVAR}} name = {fname}".format(fname = "John")

Probably the simplest way is to seperate it with a +.
Something like this:
print("{NOTVAR }" + "name = {fname}".format(fname = "John"))
{NOTVAR} name = John

Related

Regex to extract the date based on particular string [duplicate]

This question already has answers here:
Python/Regex - How to extract date from filename using regular expression?
(5 answers)
Closed 2 years ago.
am trying to extract the date if it matches to a particular regex
Ex :
string1 = '10/22/2019 from'
string2 = '12/22/2020 33455SE'
string3 = '7/20/2020 S0023'
Am trying to extract the string 2
Regex used :
r'(\d+[/]\d+[/]\d+[-\s\.]\d+)'
The above used regex is giving me if the string looks like, "10/22/2019 33455" but if there is a alphabet after as shown "33455SE", my code fails.
Any help ?
Tried codes :
r'(\d+[/]\d+[/]\d+[-\s\.]^\d+)' - Tried to use starts with.
Expected output : only string 2 and string 3
12/22/2020
7/20/2020
This works
import re
a = "3443E hello 10/22/2019 33455SE"
number = re.findall(r"[0-9]{2}[/][0-9]{2}[/][0-9]{4}",a)
print(number[0])
Output :
10/22/2019
This should work:
r'(\d+[/]\d+[/]\d+[-\s\.]\d+[A-Z]*)'
\d{1,2}/\d{2}/\d{4}(?=\s\w*\d+)
https://regex101.com/r/gCXHQ6/3

Is there a way to delete characters after # in an email address? [duplicate]

This question already has answers here:
How to remove all characters after a specific character in python?
(10 answers)
Closed 3 years ago.
I am trying to delete characters after "#" in an email address using python. I found some tips online but it didn't provide the exact solution I am looking for.
The whole email address I'm working on is coming from a parameter value.
paramater = [0] #contains the email address i.e. testing#email.com
mainStr = parameter [0]
newstring = mainStr.replace('#' , ' ')
Obviously, the above code is not providing me with result I am expecting which is to delete all strings after #.
in = 'test#gmail.com'
out = s.split('#')[0]
or
out = ''.join(re.findall('(.*)#',s))
With regex
import re
re.sub(r'#.*', '', 'test#email.com')
I don't know your code.
But, I write code by my way.
s = test#email.com
end = s.find('#')
s[:end]
Do you want a part of account? ( for example " testing" in "testing#baba.net")
Then
exampleString = "testing#baba.net"
indexOfAt = exampleString.find("#") # get index of #
print (indexOfAt)
accountPart = exampleString[:indexOfAt]
print (accountPart)
the result is as follows
7
testing

Removing all instances of [] and ' ' from files [duplicate]

This question already has answers here:
Best way to replace multiple characters in a string?
(16 answers)
How to replace two things at once in a string?
(6 answers)
Closed 3 years ago.
I have text file with the following content:
Hi, my name is 'James'. What is your [name]
I wont to remove the ' ' and the [] from the text so the output looks like this:
Hi, my name is James. What is your name
Here is my code:
s= Hi, my name is 'James'. What is your [name]
s=s.replace("[","")
s=s.replace("'","")
However the output leave a bracket to the right of name:
Hi, my name is James. What is your name]
Any ideas?
You forgot to replace ] in your original code. You can also chain replace statements together
In [2]: s= "Hi, my name is 'James'. What is your [name]"
In [3]: s = s.replace("'",'').replace("[","").replace("]","")
In [4]: s
Out[4]: 'Hi, my name is James. What is your name'
Or you can use a regex to strip of [] and ' using the regex [\[\]\'], which essentially replaces the characters []' when found in the string with an empty character using re.sub
import re
s = "Hi, my name is 'James'. What is your [name]"
out = re.sub(r"[\[\]\']", "",s)
print(out)
The output will be
Hi, my name is James. What is your name
if your trying to just put this in console or terminal then print("Hi, my name is James. What is your name? ") will work just fine
But if you are looking for a person to 'talk' and respond then use an input like,
input('Hi, my name is James. What is your name ')
the user can then type after this and if you use a variable you can respond to the users response
example
name = input('whats your name'? )
print('Hi' + name + 'How are you'? )
putting spaces after question marks will make the code look better(in my opinion) when being executed.

Python - Concat two raw strings with an user name [duplicate]

This question already has answers here:
Why can't Python's raw string literals end with a single backslash?
(13 answers)
Why can't I end a raw string with a backslash? [duplicate]
(4 answers)
Closed 3 years ago.
I have a raw string like this,
MasterFile_Name = r'C:\Users\ABC\X12345\DEF\File - Test.xlsx'
I want to pass the value of X12345 through a variable.To do that I am doing something like this
MyID = X12345
MasterFile_Name = r'C:\Users\ABC\' + MyID + '\DEF\File - Test.xlsx'
and
MasterFile_Name = r'C:\Users\ABC\' + MyID + r'\DEF\File - Test.xlsx'
They both are not working for me.
Kindly help me with this.
If the intention is to just concatenate it.
using str.format():
MyID = 'X12345'
MasterFile_Name = r'C:\Users\ABC\{}\DEF\File - Test.xlsx'.format(MyID)
print(MasterFile_Name)

Simplifying lists in python? [duplicate]

This question already has answers here:
Check if multiple strings exist in another string
(17 answers)
Closed 5 years ago.
I'm new to coding, and am wondering if there is a way to simplify this:
if "!" in name or "$" in name or "%" in name or "^" in name or "&" in name or "*" in name or "(" in name or ")" in name or "_" in name or "-" in name or "+" in name or "=" in name:
points = points + 1
Thank you
You can use regex:
import re
if re.findall('[\!\$\%\^\&\*\_\-\+\=]', name):
points += 1
chars = set("!$%^&*()_-+=")
if any((i in chars) for i in string):
points += 1
You can write a simple function to achieve this.
def is_included(characters, sentence):
for character in characters:
if character in sentence:
return True
return False
As mgilson mention you can use any keyword too. However since you are new to programming I suggest you to learn the algorithms of keywords.

Categories

Resources