Say I have two strings, string1="A B C " and string2="abc". How do combine these two strings so string1 becomes "AaBbCc"? So basically I want all the spaces in string1 to be replaced by characters in string2. I tried using two for-loops like this:
string1="A B C "
string2="abc"
for char1 in string1:
if char1==" ":
for char2 in string2:
string1.replace(char1,char2)
else:
pass
print(string1)
But that doesn't work. I'm fairly new to Python so could somebody help me? I use version Python3. Thank you in advance.
You can use iter on String2 and replace ' ' with char in String2 like below:
>>> string1 = "A B C "
>>> string2 = "abc"
>>> itrStr2 = iter(string2)
>>> ''.join(st if st!=' ' else next(itrStr2) for st in string1)
'AaBbCc'
If maybe len in two String is different you can use itertools.cycle like below:
>>> from itertools import cycle
>>> string1 = "A B C A B C "
>>> string2 = "abc"
>>> itrStr2 = cycle(string2)
>>> ''.join(st if st!=' ' else next(itrStr2) for st in string1)
'AaBbCcAaBbCc'
string1 = "A B C "
string2 = "abc"
out, repl = '', list(string2)
for s in string1:
out += s if s != " " else repl.pop(0)
print(out) #AaBbCc
Related
i have a few strings that hold a value. i.e.
how can i loop though multiple string1,string2,string3 etc?
string1 = re.findall('qr="">(.*?)</span', str(raw[1]))
string2 = re.findall('qr="">(.*?)</span', str(raw[2]))
string3 = re.findall('qr="">(.*?)</span', str(raw[3]))
for i in x:
print(i)
i would like for it to print the value of string1,string2,string3
i have tried to store string1 - string3 in a list but with ut success.
Try Something like this perhaps
strings = []
string1 = re.findall('qr="">(.*?)</span', str(raw[1]))
string2 = re.findall('qr="">(.*?)</span', str(raw[2]))
string3 = re.findall('qr="">(.*?)</span', str(raw[3]))
strings.append(string1)
strings.append(string2)
strings.append(string3)
for i in strings:
print(i)
I would like to pull out the locations for an inconsistently formatted data field in a Pandas dataframe. (I do not maintain the data so I cannot alter how this field is formatted.)
Running the following toy version
string2 = 'Denver.John'
if string2.find(' -'):
string2 = string2.split(' -')[0]
elif string2.find('.'):
string2 = string2.split('.')[0]
print(string2)
gives me Denver.John instead of Denver. However, if I use an if instead:
string2 = 'Denver.John'
if string2.find(' -'):
string2 = string2.split(' -')[0]
if string2.find('.'):
string2 = string2.split('.')[0]
print(string2)
I get Denver, as desired. The problem is I also have strings like 'Las.Vegas - Rudy' and I want to be able to pull out Las.Vegas in those instances so I only want to split on a period if the field does not contain the hyphen (' - ').
Why does the elif not work for Denver.John?
Because find either yields the index or -1 while -1 is valid!!!, so try using:
string2 = 'Denver.John'
if string2.find(' -') + 1:
string2 = string2.split(' -')[0]
elif string2.find('.') + 1:
string2 = string2.split('.')[0]
print(string2)
Or better like:
string2 = 'Denver.John'
if ' -' in string2:
string2 = string2.split(' -')[0]
elif '.' in string2:
string2 = string2.split('.')[0]
print(string2)
Use
if ' -' in string2
instead. The find method returns an int
find() returns the lowest index of the substring if it is found in given string. If it’s not found then it returns -1.
So in your case:
string2 = 'Denver.John'
print(string2.find(' -')) # prints -1
print(string2.find('.')) # prints 6
if string2.find(' -'):
string2 = string2.split(' -')[0]
elif string2.find('.'):
string2 = string2.split('.')[0]
print(string2)
So in your if statement you can compare the result of find with -1.
string.find returns a position of the substring, and it is -1 if it doesn't find the substring.
Thus, do the following instead:
string2 = 'Denver.John'
if string2.find(' -') >= 0:
string2 = string2.split(' -')[0]
elif string2.find('.') >= 0:
string2 = string2.split('.')[0]
print(string2)
Say I have strings,
string1 = 'Hello how are you'
string2 = 'are you doing now?'
The result should be something like
Hello how are you doing now?
I was thinking different ways using re and string search.
(Longest common substring problem)
But is there any simple way (or library) that does this in python?
To make things clear i'll add one more set of test strings!
string1 = 'This is a nice ACADEMY'
string2 = 'DEMY you know!'
the result would be!,
'This is a nice ACADEMY you know!'
This should do:
string1 = 'Hello how are you'
string2 = 'are you doing now?'
i = 0
while not string2.startswith(string1[i:]):
i += 1
sFinal = string1[:i] + string2
OUTPUT :
>>> sFinal
'Hello how are you doing now?'
or, make it a function so that you can use it again without rewriting:
def merge(s1, s2):
i = 0
while not s2.startswith(s1[i:]):
i += 1
return s1[:i] + s2
OUTPUT :
>>> merge('Hello how are you', 'are you doing now?')
'Hello how are you doing now?'
>>> merge("This is a nice ACADEMY", "DEMY you know!")
'This is a nice ACADEMY you know!'
This should do what you want:
def overlap_concat(s1, s2):
l = min(len(s1), len(s2))
for i in range(l, 0, -1):
if s1.endswith(s2[:i]):
return s1 + s2[i:]
return s1 + s2
Examples:
>>> overlap_concat("Hello how are you", "are you doing now?")
'Hello how are you doing now?'
>>>
>>> overlap_concat("This is a nice ACADEMY", "DEMY you know!")
'This is a nice ACADEMY you know!'
>>>
Using str.endswith and enumerate:
def overlap(string1, string2):
for i, s in enumerate(string2, 1):
if string1.endswith(string2[:i]):
break
return string1 + string2[i:]
>>> overlap("Hello how are you", "are you doing now?")
'Hello how are you doing now?'
>>> overlap("This is a nice ACADEMY", "DEMY you know!")
'This is a nice ACADEMY you know!'
If you were to account for trailing special characters, you'd be wanting to employ some re based substitution.
import re
string1 = re.sub('[^\w\s]', '', string1)
Although note that this would remove all special characters in the first string.
A modification to the above function which will find the longest matching substring (instead of the shortest) involves traversing string2 in reverse.
def overlap(string1, string2):
for i in range(len(s)):
if string1.endswith(string2[:len(string2) - i]):
break
return string1 + string2[len(string2) - i:]
>>> overlap('Where did', 'did you go?')
'Where did you go?'
Other answers were great guys but it did fail for this input.
string1 = 'THE ACADEMY has'
string2= '.CADEMY has taken'
output:
>>> merge(string1,string2)
'THE ACADEMY has.CADEMY has taken'
>>> overlap(string1,string2)
'THE ACADEMY has'
However there's this standard library difflib which proved to be effective in my case!
match = SequenceMatcher(None, string1,\
string2).find_longest_match\
(0, len(string1), 0, len(string2))
print(match) # -> Match(a=0, b=15, size=9)
print(string1[: match.a + match.size]+string2[match.b + match.size:])
output:
Match(a=5, b=1, size=10)
THE ACADEMY has taken
which words you want to replace are appearing in the second string so you can try something like :
new_string=[string2.split()]
new=[]
new1=[j for item in new_string for j in item if j not in string1]
new1.insert(0,string1)
print(" ".join(new1))
with the first test case:
string1 = 'Hello how are you'
string2 = 'are you doing now?'
output:
Hello how are you doing now?
second test case:
string1 = 'This is a nice ACADEMY'
string2 = 'DEMY you know!'
output:
This is a nice ACADEMY you know!
Explanation :
first, we are splitting the second string so we can find which words we have to remove or replace :
new_string=[string2.split()]
second step we will check each word of this splitter string with string1 , if any word is in that string than choose only first string word , leave that word in second string :
new1=[j for item in new_string for j in item if j not in string1]
This list comprehension is same as :
new1=[]
for item in new_string:
for j in item:
if j not in string1:
new1.append(j)
last step combines both string and join the list:
new1.insert(0,string1)
print(" ".join(new1))
input:
[1] string1 [2] string 2[3]string3 [5]string4
output:
string1
string 2
string 4
how to resolve the case as "input" and the result "output" with "regex" python?
Your instructions are a bit vague, that said - using the example input you provided:
import re
line = '[1] string1 [2] string 2[3]string3 [5]string4'
matches = re.match(r'\[1\] (.*?)\[2\] (.*?)\[3\](.*?)\[5\](.*)', line)
print matches.group(1) # prints string1
print matches.group(2) # prints string 2
print matches.group(3) # prints string3
print matches.group(4) # prints string4
It seems like you don't want the value of an index if it is preceded by a character other than white space or a starting anchor ,
>>> s = "[1] string1 [2] string 2[3]string3 [5]string4"
>>> import regex
>>> m = regex.findall(r'(?<= |^)\[\d+\]\s*([^\[]*)', s)
>>> for i in m:
... print i
...
string1
string 2
string4
I need to know if there is a function that detects the lowercase letters in a string. Say I started writing this program:
s = input('Type a word')
Would there be a function that lets me detect a lowercase letter within the string s? Possibly ending up with assigning those letters to a different variable, or just printing the lowercase letters or number of lowercase letters.
While those would be what I would like to do with it I'm most interested in how to detect the presence of lowercase letters. The simplest methods would be welcome.
To check if a character is lower case, use the islower method of str. This simple imperative program prints all the lowercase letters in your string:
for c in s:
if c.islower():
print c
Note that in Python 3 you should use print(c) instead of print c.
Possibly ending up with assigning those letters to a different variable.
To do this I would suggest using a list comprehension, though you may not have covered this yet in your course:
>>> s = 'abCd'
>>> lowercase_letters = [c for c in s if c.islower()]
>>> print lowercase_letters
['a', 'b', 'd']
Or to get a string you can use ''.join with a generator:
>>> lowercase_letters = ''.join(c for c in s if c.islower())
>>> print lowercase_letters
'abd'
There are 2 different ways you can look for lowercase characters:
Use str.islower() to find lowercase characters. Combined with a list comprehension, you can gather all lowercase letters:
lowercase = [c for c in s if c.islower()]
You could use a regular expression:
import re
lc = re.compile('[a-z]+')
lowercase = lc.findall(s)
The first method returns a list of individual characters, the second returns a list of character groups:
>>> import re
>>> lc = re.compile('[a-z]+')
>>> lc.findall('AbcDeif')
['bc', 'eif']
There are many methods to this, here are some of them:
Using the predefined str method islower():
>>> c = 'a'
>>> c.islower()
True
Using the ord() function to check whether the ASCII code of the letter is in the range of the ASCII codes of the lowercase characters:
>>> c = 'a'
>>> ord(c) in range(97, 123)
True
Checking if the letter is equal to it's lowercase form:
>>> c = 'a'
>>> c.lower() == c
True
Checking if the letter is in the list ascii_lowercase of the string module:
>>> from string import ascii_lowercase
>>> c = 'a'
>>> c in ascii_lowercase
True
But that may not be all, you can find your own ways if you don't like these ones: D.
Finally, let's start detecting:
d = str(input('enter a string : '))
lowers = [c for c in d if c.islower()]
# here i used islower() because it's the shortest and most-reliable
# one (being a predefined function), using this list comprehension
# is (probably) the most efficient way of doing this
You should use raw_input to take a string input. then use islower method of str object.
s = raw_input('Type a word')
l = []
for c in s.strip():
if c.islower():
print c
l.append(c)
print 'Total number of lowercase letters: %d'%(len(l) + 1)
Just do -
dir(s)
and you will find islower and other attributes of str
import re
s = raw_input('Type a word: ')
slower=''.join(re.findall(r'[a-z]',s))
supper=''.join(re.findall(r'[A-Z]',s))
print slower, supper
Prints:
Type a word: A Title of a Book
itleofaook ATB
Or you can use a list comprehension / generator expression:
slower=''.join(c for c in s if c.islower())
supper=''.join(c for c in s if c.isupper())
print slower, supper
Prints:
Type a word: A Title of a Book
itleofaook ATB
If you don't want to use the libraries and want simple answer then the code is given below:
def swap_alpha(test_string):
new_string = ""
for i in test_string:
if i.upper() in test_string:
new_string += i.lower()
elif i.lower():
new_string += i.upper()
else:
return "invalid "
return new_string
user_string = input("enter the string:")
updated = swap_alpha(user_string)
print(updated)