This question already has answers here:
Character Translation using Python (like the tr command)
(6 answers)
Closed 2 years ago.
In my project I need to be able to replace a regex in a string to another.
For example if I have 2 regular expressions [a-c] and [x-z], I need to be able to replace the string "abc" with "xyz", or the string "hello adan" with "hello xdxn". How would I do this?
We build a map and then translate letter by letter.
When using get for dictionary then the second argument specifying what to return if not find.
>>> trans = dict(zip(list("xyz"),list("abc")))
>>> trans
{'x': 'a', 'y': 'b', 'z': 'c'}
>>> "".join([trans.get(i,i) for i in "hello xdxn"])
'hello adan'
>>>
Or change the order in trans to go in other direction
>>> trans = dict(zip(list("abc"),list("xyz")))
>>> trans
{'a': 'x', 'b': 'y', 'c': 'z'}
>>> "".join([trans.get(i,i) for i in "hello adan"])
'hello xdxn'
>>>
Try with re.sub
>>>replace = re.sub(r'[a-c]+', 'x','Hello adan')
>>>replace
'Hello xdxn'
>>>re.sub(r'[a-c]+', 'x','Hello bob')
'Hello xox'
Related
This question already has answers here:
Replacing one character of a string in python
(6 answers)
Closed 5 years ago.
I am working in Python right now.
My ch variable, which is the character variable has a character store in it, which has been entered by the user.
I also have a string variable (string1) in which I want to add the character without over-writing the string variable.
i.e I want to do string1[i]=ch, where i can be any position.
when i do this in python, it gives an error saying: 'str' object does not support item assignment.
here, string1[i]=ch, is in a while loop.
Is there a proper way of doing this? Please help.
str in python is immutable. That means, you can't assign some random value to a specific index.
For example, below is not possible:
a = 'hello'
a[2]= 'X'
There is an workaround.
make a list from the str.
mutate the specific index you wanted from that list.
form a str again from the list.
Like below:
>>> a = 'hello'
>>> tmp_list = list(a)
>>> tmp_list
['h', 'e', 'l', 'l', 'o']
>>> tmp_list[2] = 'X'
>>> tmp_list
['h', 'e', 'X', 'l', 'o']
>>>
>>> a = ''.join(tmp_list)
>>> a
'heXlo'
Say I have the following string
>>> mystr = 'A-ABd54-Bf657'
(a random string of dash-delimited character groups) and want to match the opening part, and the rest of the string, in separate groups. I can use
>>> re.match('(?P<a>[a-zA-Z0-9]+)-(?P<b>[a-zA-Z0-9-]+)', mystr)
This produces a groupdict() like this:
{'a': 'A', 'b': 'ABd54-Bf657'}
How can I get the same regex to match group b but separately match a specific suffix (or set of suffices) if it exists (they exist)? Ideally something like this
>>> myregex = <help me here>
>>> re.match(myregex, 'A-ABd54-Bf657').groupdict()
{'a': 'A', 'b': 'ABd54-Bf657', 'test': None}
>>> re.match(myregex, 'A-ABd54-Bf657-blah').groupdict()
{'a': 'A', 'b': 'ABd54-Bf657-blah', 'test': None}
>>> re.match(myregex, 'A-ABd54-Bf657-test').groupdict()
{'a': 'A', 'b': 'ABd54-Bf657', 'test': 'test'}
Thanks.
mystr = 'A-ABd54-Bf657'
re.match('(?P<a>[a-zA-Z0-9]+)-(?P<b>[a-zA-Z0-9-]+?)(?:-(?P<test>test))?$', mystr)
^ ^
The first indicated ? makes the + quantifier non-greedy, so that it consumes the minimum possible.
The second indicated ? makes the group optional.
The $ is necessary or else the non-greediness plus optionality will match nothing.
How does the following output come?
>>> a
'hello'
>>> a = list(a)
>>> a
['h', 'e', 'l', 'l', 'o']
>>> a = str(a)
>>> a
"['h', 'e', 'l', 'l', 'o']"
>>> a.title()
"['H', 'E', 'L', 'L', 'O']"
>>> a[0]
'['
>>> a[1]
"'"
>>> a[2]
'h'
When title has to capitalize only the first letter of the string, how does every letter get capitalized?
str() does not join a list of individual characters back together into a single string. You'd use str.join() for that:
>>> a = list('hello')
>>> ''.join(a)
'hello'
str(listobject) returns a string representation of the list object, not the original string you converted to a list. The string representation is a debug tool; text you can, for the most part, paste back into a Python interpreter and have it recreate the original data.
If you wanted to capitalise just the first characters, use str.title() directly on the original string:
>>> 'hello'.title()
'Hello'
>>> 'hello world'.title()
'Hello World'
I think you're confused about how title works.
In [5]: s = "hello there"
In [6]: s.title()
Out[6]: 'Hello There'
See how it capitalises the first letter of each word? When you str() the list, it no longer sees hello as a single word. Instead, it sees each letter on its own and decides to capitalise each letter.
>>> a=list('hello')
>>> str(a)
"['h', 'e', 'l', 'l', 'o']"
>>> len(str(a))
25
So you have a string of 25 characters. All those ',, etc. are part of the string. title sees 5 one-character words, so each one is upper cased. Try ''.join instead
>>> ''.join(a)
'hello'
>>> len(''.join(a))
5
>>> ''.join(a).title()
'Hello'
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to create a list with the characters of a string?
Example:
'abc'
becomes
['a', 'b', 'c']
Is it a combination of split and slicing?
>>> x = 'abc'
>>> list(x)
['a', 'b', 'c']
Not sure what you are trying to do, but you can access individual characters from a string itself:
>>> x = 'abc'
>>> x[1]
'b'
If you need to iterate over the string you do not even need to convert it to a list:
>>> n = 'abc'
>>> for i in n:
... print i
...
a
b
c
or
>>> n[1]
'b'
yourstring = 'abc'
[char for char in yourstring]
This question already has answers here:
How do I split a string into a list of characters?
(15 answers)
Closed 6 years ago.
How can I turn a string (like 'hello') into a list (like [h,e,l,l,o])?
The list() function [docs] will convert a string into a list of single-character strings.
>>> list('hello')
['h', 'e', 'l', 'l', 'o']
Even without converting them to lists, strings already behave like lists in several ways. For example, you can access individual characters (as single-character strings) using brackets:
>>> s = "hello"
>>> s[1]
'e'
>>> s[4]
'o'
You can also loop over the characters in the string as you can loop over the elements of a list:
>>> for c in 'hello':
... print c + c,
...
hh ee ll ll oo