I have the following text:
ABC=ABC.2016.001.02.Yomama.01234
How to lowercase just the Yomama part. I'd like it to look like this:
ABA.2016.001.02.yomama.01234
How can I accomplish this with python?
Any help would be appreciated. Thanks.
Assuming that you want a generic solution (otherwise you could just use str.replace() with a hard coded string) you can split the string on the ., lowercase the string in the appropriate field, and then stitch it back together with str.join():
s = 'ABC=ABC.2016.001.02.Yomama.01234'
fields = s.split('.')
fields[4] = fields[4].lower()
print('.'.join(fields))
Alternative solution, provided text ABC don't have repeating text
tmp = ABC.split('.')[-2]
ABC = ABC.replace(tmp, tmp.lower())
Related
Let's say I have a string defined like this:
string1 = '23h4b245hjrandomstring345jk3n45jkotherrandomstring'
The goal is to grab the 11 characters (these for example '345jk3n45jk') after a part of the string (this part for example 'randomstring') using a specified search term and the specified number of characters to grab after that search term.
I tried doing something like this:
string2 = substring(string1,'randomstring', 11)
I appreciate any help you guys have to offer!
string2 = string1[string1.find("randomstring")+len("randomstring"):string1.find("randomstring")+len("randomstring")+11]
In one line, using split, and supposing that your randomstring is unique in your string, which seems to be the case as you worded out the question :
string1 = '23h4b245hjrandomstring345jk3n45jkotherrandomstring'
randomstring = 'randomstring'
nb_char_to_take = 11
# split using randomstring as splitter, take part of the string after, i.e the second part of the array, and then the 11 first character
result = string1.split(randomstring)[1][:nb_char_to_take]
You can use a simple regular expression like this
import re
s = "23h4b245hjrandomstring345jk3n45jkotherrandomstring"
result = re.findall("randomstring(.{11})", s)[0]
string1 = '23h4b245hjrandomstring345jk3n45jkotherrandomstring'
string2 = string1[10:22]
print(string2)
randomstring
You could use that. Its called string slicing, you basically count the position of the letters and then the first number before the colon is your starting point the second is your ending point when you enter those position numbers you should get whatever is in-between those position, the last is for a different function I highly suggest you search string slicing on YouTube as my explanation wouldn't really help you, and also search up * Find string method* those should hep you get the idea behind those functions. Sorry couldn't be of much help hope the videos help.
I have f = imgString.split('medias/')[1] g = f.split('?')[0] print(g) but I'd prefer it on one line. How can I split this string into multiple parts 'media/Clearance.png?sometexthere' .Ideally I'd like just the Clearance.png. so if I was splitting it it'd be 'media/', 'Clearance.png' and '?sometexthere'
string = 'media/Clearance.png?sometexthere'
string.split("/")[1].split("?")[0]
If it is always the same format you can use regex like this one :
([a-zA-Z]*)\/(.*)\?([a-zA-Z]*) and then with re.group() you can have all the parts of your string :)
You can check it here link !
How can we get the substring based on fullstops using regex? We only wish to get the data after the full stop
Str = “i like cows. I also like camels”
// Regex Code here
Output : “I also like camels”
No need to use regex for that. Use split() method.
splitted = Str.split('.')
# splitted[0] will be 'i like cows'
# splitted[1] will be 'I also like camels'
You can use this approach:
str1 = 'i like cows. I also like camels'
print(str1.split('.')[1:][0].strip())
output:
I also like camels
Try this split
String dataIWant = mydata.split(".")[1];
Result : I also like camels
Using split('.') and selecting the last element is generally better but for fun this is a RegEx solution:
import re
Str = "i like .cows. I also like camels"
pattern = r"([^\.]*$)"
results = re.search(pattern, Str)
print(results.group(1).strip())
This (?:[.]\s([A-Z].+)) picks "I also like camels"
i was wondering if anyone has a simpler solution to extract a few letters in the middle of a string. i want to retrive the 3 letters (in this case, GMB) and all the entries follow the same patter. i'struggling o get a simpler way of doing this.
here is an example of what i've been using.
entry = "entries-alphabetical.jsp?raceid13=GMB$20140313A"
symbol = entry.strip('entries-alphabetical.jsp?raceid13=')
symbol = symbol[0:3]
print symbol
thanks
First of all the argument passed to str.strip is not prefix or suffix, it is just a combination of characters that you want to be stripped off from the string.
Since the string looks like an url, you can use urlparse.parse_qsl:
>>> import urlparse
>>> urlparse.parse_qsl(entry)
[('entries-alphabetical.jsp?raceid13', 'GMB$20140313A')]
>>> urlparse.parse_qsl(entry)[0][1][:3]
'GMB'
This is what regular expressions are for. http://docs.python.org/2/library/re.html
import re
val = re.search(r'(GMB.*)', entry)
print val.group(1)
I am looking to accomplish the following and am wondering if anyone has a suggestion as to how best go about it.
I have a string, say 'this-is,-toronto.-and-this-is,-boston', and I would like to convert all occurrences of ',-[a-z]' to ',-[A-Z]'. In this case the result of the conversion would be 'this-is,-Toronto.-and-this-is,-Boston'.
I've been trying to get something working with re.sub(), but as yet haven't figured out how how
testString = 'this-is,-toronto.-and-this-is,-boston'
re.sub(r',_([a-z])', r',_??', testString)
Thanks!
re.sub can take a function which returns the replacement string:
import re
s = 'this-is,-toronto.-and-this-is,-boston'
t = re.sub(',-[a-z]', lambda x: x.group(0).upper(), s)
print t
prints
this-is,-Toronto.-and-this-is,-Boston