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

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)

Related

Compilation of complex regex expressions [duplicate]

This question already has answers here:
String formatting in Python [duplicate]
(14 answers)
Closed 2 years ago.
I'm trying to understand the following code related to complex regex.
I do not understand how the full_regex line operates? What is the use of the '%s' as well as the other % before the (regex1, regex2...)
Can someone please help with this?
regex1 = '(\d{1,2}[/-]\d{1,2}[/-]\d{2,4})'
regex2 = '((?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[\S]*[+\s]\d{1,2}[,]{0,1}[+\s]\d{4})'
regex3 = '(\d{1,2}[+\s](?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[\S]*[+\s]\d{4})'
regex4 = '((?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[\S]*[+\s]\d{4})'
regex5 = '(\d{1,2}[/-][1|2]\d{3})'
regex6 = '([1|2]\d{3})'
full_regex = '(%s|%s|%s|%s|%s|%s)' %(regex1, regex2, regex3, regex4, regex5, regex6)
The expression
full_regex = '(%s|%s|%s|%s|%s|%s)' % (regex1, regex2, regex3, regex4, regex5, regex6)
just merges all of the other regexps into one big one that alternates between all of them; that's not regex syntax, it's just Python string interpolation.

How to split a string without a delimiter [duplicate]

This question already has answers here:
How do I get a substring of a string in Python? [duplicate]
(16 answers)
Closed 2 years ago.
How can I split the string '07:05:45PM' into ['07:05:45', 'PM'] in Python?
Have you tried something like:
a = ‘07:05:45PM‘
b = [a[:-2], a[-2:]]

Split the string into words [duplicate]

This question already has answers here:
Split string every nth character?
(19 answers)
Closed 4 years ago.
how to split a string into words of 2 letters. Like given string is "HelloThere" now i want to make it ["He","ll","oT","he","re"]. Please help to code that in python.
yourList = []
yourString = "HelloThere"
while yourString:
yourList.append(yourString[:2])
yourString = yourString[2:]
If you print yourList, you will get the result.

Python regex with variable {}-multiplier [duplicate]

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.
Say you wanted to create a pattern that matches sequences of var consecutive digits. You could do it this way:
p = re.compile(r"\d{"+str(var)+"}")
or this way:
p = re.compile(r"\d{%d}" % var)
But how would you do it using format()?
I tried both:
p = re.compile(r"\d{0}".format(var))
and:
p = re.compile(r"\d{{0}}".format(var))
but none of these worked.
You need to actually have triple { and } - two for the escaped literal braces and one for the placeholder:
In [1]: var = 6
In [2]: r"\d{{{0}}}".format(var)
Out[2]: '\\d{6}'

Eliminate numbers in string in Python [duplicate]

This question already has answers here:
Remove specific characters from a string in Python
(26 answers)
Removing numbers from string [closed]
(8 answers)
Closed 8 years ago.
I’d like to eliminate numbers in a string in Python.
str = "aaaa22222111111kkkkk"
I want this to be "aaaakkkkk".
I use re.sub to replace, but it doesn't work:
str = "aaaa22222111111kkkkk"
str = re.sub(r'^[0-9]+$',"",str)
Maybe, this replaces a string which only contains numbers with "".
How should I do with this?
your regex is wrong:
re.sub(r'[0-9]',"",str)
should work:
>>> str="aaaa22222111111kkkkk"
>>> re.sub(r'[0-9]',"",str)
'aaaakkkkk'

Categories

Resources