This question already has answers here:
How can I split and parse a string in Python? [duplicate]
(3 answers)
Closed 3 years ago.
I know that in Python you can use the array selector to retrieve a certain part of a string, ie me.name[10:] to get just the last 10 characters.
but how would you retrieve just the part of a string after an underscore ie _ using a single expression?
For example if my string is "stringcharThatChange_myname"
How would I extract just 'myname' ? I'm confined to using Python 3.5.1
You could use split.
test_string = "stringcharThatChange_myname"
print(test_string.split('_')[1]) # myname
Using split method.
"this will be excluded_this is kept".split('_')[1]
Related
This question already has answers here:
How do I validate a date string format in python?
(5 answers)
Closed 6 months ago.
Im trying to check if a user's input is following the pattern integer/integer/integer(like month/day/year) but i dont know how to use exactly the match function to define that the pattern contains "number",then "/",again "number" and "/"...
Check out https://regex101.com/ for a neat website to check your regex! This is implemented in python using the re library. https://docs.python.org/3/library/re.html
In your case, the pattern would be [0-9]{1,2}\/[0-9]{1,2}\/[0-9]{2,4}
This question already has answers here:
How to write string literals in Python without having to escape them?
(6 answers)
Closed last year.
I want to assing a path to a variable a:
a = "D:\misc\testsets\Real"
How can i omit the \t metacharacter without changing the folder name?
Use raw strings:
a = r"D:\misc\testsets\Real"
Try this:
r denotes raw string.
a = r"D:\misc\testsets\Real"
This question already has answers here:
How can I split and parse a string in Python? [duplicate]
(3 answers)
Closed 1 year ago.
I am trying to make python take a string, separate the characters before another character eg: "10001001010Q1002000293Q100292Q". I want to separate the string before each Q and have python create either a list or another string. I cannot figure this out for the life of me.
You can do this using the split function, give "Q" as a parameter to the split function then you can slice the list to only get the numbers before Q.
num = "10001001010Q1002000293Q100292Q"
print(num.split("Q")[:-1])
Split() function: https://www.w3schools.com/python/ref_string_split.asp
Slicing: https://www.w3schools.com/python/python_strings_slicing.asp
The syntax is str.split("separator").
str = str.split("Q")
Then output will be ['10001001010', '1002000293', '100292', ''].
If you don't need the last empty element then you can write as:
str = str.split("Q")[:-1]
This question already has answers here:
How to use regex to find all overlapping matches
(5 answers)
Closed 2 years ago.
I have text as below-
my_text = "My telephone number is 408-555-1234"
on which i am searching the pattern
re.findall(r'\d{3}-\d{1,}',my_text)
My intention was to search for three digit numeric value followed by - and then another set of one or more than one digit numeric value. Hence I was expecting the result to be - ['408-555','555-1234'],
However the result i am getting os only ['408-555'] .
Could anyone suggest me what is wrong in my understaning here. And suggest a pattern that would serve my purpose
you can use:
re.findall(r'(?=(\d{3}-\d+))', my_text)
output:
['408-555', '555-1234']
This question already has answers here:
why is python string split() not splitting
(3 answers)
Closed 6 years ago.
I'm trying to split
<team>
into just team, here is the code I'm using:
s = "<team>"
s.split(">")[1]
s
'<team>'
s.split(">")[1].split("<")[0]
s
'<team>
As you can see, it's still leaving me with
<team>
anyone know why>
str.split() function returns a list, it does not split the string in place.
You'll need to make a new variable:
s = "<team>"
t = s.split(">")[1]
t