Conversion of a specific character in a string to Upper Case - python

How can I convert a specific letter in a string, i.e all the the as in 'ahdhkhkahfkahafafkh' to uppercase?
I can only seem to find ways to capitalize the first word or upper/lower case the entire string.

You can use str.translate with string.maketrans:
>>> import string
>>> table = string.maketrans('a', 'A')
>>> 'abcdefgahajkl'.translate(table)
'AbcdefgAhAjkl'
This really shines if you want to replace 'a' and 'b' with their uppercase versions... then you just change the translation table:
table = string.maketrans('ab', 'AB')
Or, you can use str.replace if you really are only doing a 1 for 1 swap:
>>> 'abcdefgahajkl'.replace('a', 'A')
'AbcdefgAhAjkl'
This method shines when you only have one replacement. It replaces substrings rather than characters, so 'Bat'.replace('Ba', 'Cas') -> 'Cast'.

'banana'.replace('a', "A")
From the docs: https://docs.python.org/2/library/string.html#string.replace

>>> a = 'ahdhkhkahfkahafafkh'
>>> "".join(i.upper() if i == 'a' else i for i in a)
'AhdhkhkAhfkAhAfAfkh'
Or
>>> a.replace('a',"A")
'AhdhkhkAhfkAhAfAfkh'

Related

Replacing unknown characters in a string Python 2.7

How can I define characters(in a LIST or a STRING), and have any other characters replaced with.. lets say a '?'
Example:
strinput = "abcdefg#~"
legal = '.,/?~abcdefg' #legal characters
while i not in legal:
#Turn i into '?'
print output
Put the legal characters in a set then use in to test each character of the string. Construct the new string using the str.join() method and a conditional expression.
>>> s = "test.,/?~abcdefgh"
>>> legal = set('.,/?~abcdefg')
>>> s = ''.join(char if char in legal else '?' for char in s)
>>> s
'?e??.,/?~abcdefg?'
>>>
If this is a large file, read in chunks, and apply re.sub(..) as below. ^ within a class (square brackets) stands for negation (similar to saying "anything other than")
>>> import re
>>> char = '.,/?~abcdefg'
>>> re.sub(r'[^' + char +']', '?', "test.,/?~abcdefgh")
'?e??.,/?~abcdefg?'

python command string.replace(old, lower(), 'something') returns old string

s1 = 'GSHMGLYELSASNFELHVAQGDHFIKFFAPWCGHCKALAPTWEQLALGLEHSETVKIGKVDbTQHYELbSGNQVRGYPTLLWFRDGKKVDQYKGKRDLESLREYVESQLQR'
This is a string I would like to replace the lowercase letters to a certain uppercase letter, say, 'C'. the command I am using is :
string.replace(s1, s1.lower(), 'C'),
problem:the resulting string is still the same as the old one, b is 'b' and not 'C'
Currently, you're trying to replace a lowercase copy of the entire string with 'C'. You're also seemingly not assigning the result of string.replace with anything, which won't work. replace doesn't modify in place, it returns a new copy of the string with the replacements applied.
You'll need to iterate over the string and replace any lowercase letters.
s1 = 'GSHMGLYELSASNFELHVAQGDHFIKFFAPWCGHCKALAPTWEQLALGLEHSETVKIGKVDbTQHYELbSGNQVRGYPTLLWFRDGKKVDQYKGKRDLESLREYVESQLQR'
replaced_string = ''.join(x if x.isupper() else 'C' for x in s1)
Your condition is too complex for simple "replace" method. Use regexp instead:
import re
s1 = "GaHMxLYELmASNFElHVAQG"
s2 = re.sub(r"[a-z]", "C", s1)
print s2
It will print "GCHMCLYELCASNFECHVAQG"
[a-z] means "any letter from a to z" - add extra lower letters for it for your language, if needed. For example, for russian this pattern will be: [a-zа-я]
string.replace(s1, s1.lower(), 'C')
Will replace with 'C' only the whole string
gshmglyelsasnfelhvaqgdhfikffapwcghckalaptweqlalglehsetvkigkvdbtqhyelbsgnqvrgyptllwfrdgkkvdqykgkrdleslreyvesqlqr
If you want to substitute all the characters with a given property in a string what I suggest is to use regular expressions, in your case it will be:
s2 = re.sub("[a-z]", "C", s1)
s1.lower() is equal to
>>> s1.lower()
'gshmglyelsasnfelhvaqgdhfikffapwcghckalaptweqlalglehsetvkigkvdbtqhyelbsgnqvrgyptllwfrdgkkvdqykgkrdleslreyvesqlqr'
So string.replace(s1, s1.lower(), 'C') searches string c1 for any occurances of that whole string of lower case characters, and if it finds any then it replaces each one with 'C'.
Note that string.replace is also a method on strings themselves ever since Python 2.0 or so, s1.replace(s1.lower(), 'C') would do the exact same thing.
You can use a translation table:
>>> from string import maketrans, lowercase
>>> trans_table = maketrans(lowercase, 'C' * len(lowercase))
>>> s1.translate(trans_table)
Maketrans takes two strings of characters with equal lengths, and translate() then translates each occurence of a character in the first to its equivalent in the second.
lowercase is 'abcdefghijklmnopqrstuvwxyz', and 'C' * len(lowercase) is simply a string of 26 Cs.

Python, can't replace generator object

I need to change replace a string's punctuation marks with space.
The problem is that I need to do it in one line.
for example: there's a string: 'H,+-=/e^##%ll-!!..o'
the result should be : 'H-----e----ll-----o'
where '-' symbolizes ' ' (space)
when I do
replace((c for c in string.punctuation),' ')
I get the error:
TypeError: Can't convert 'generator' object to str implicitly
I tried to put it in a list, in a set even in a dict.
but this error keeps on coming back.
how can I surpass this?
str.replace() doesn't take a list or generator, it'd only take a string, and even then won't do what you want. The method replaces one whole sequence of characters with another, so even x.replace(string.puntuation, '-') would only replace whole occurrences of the string.punctuation string in x with one dash.
Use string.maketrans() and str.translate() instead:
import string
translationmap = string.maketrans(string.punctuation, '-' * len(string.punctuation))
x = x.translate(translationmap)
Demo:
>>> import string
>>> x = 'H,+-=/e^##%ll-!!..o'
>>> import string
>>> translationmap = string.maketrans(string.punctuation, '-' * len(string.punctuation))
>>> x.translate(translationmap)
'H-----e----ll-----o'
str.translate() is hands-down the fastest method to map characters to other characters, or delete characters from a string.
On Python 3, str.translate() (or in Python 2, unicode.translate()) takes a mapping instead:
translationmap = {ord(c): '-' for c in string.punctuation}
x.translate(translationmap)
Try following
import string
''.join(map(lambda x : '-' if x in string.punctuation else x,
'H,+-=/e^##%ll-!!..o'))
You could also use re.sub for this:
>>> from re import sub
>>> sub("\W", "-", "H,+-=/e^##%ll-!!..o")
'H-----e----ll-----o'
>>>
\W captures all non-word characters.
Note that the above code will keep underscores. If you don't want them, replace \W with [\W_].

Replace string content with each others

I have a string: 1x22x1x.
I need to replace all 1 to 2 and vice versa. So example line would be 2x11x2x. Just wondering how is it done. I tried
a = "1x22x1x"
b = a.replace('1', '2').replace('2', '1')
print b
output is 1x11x1x
Maybe i should forget about using replace..?
Here's a way using the translate method of a string:
>>> a = "1x22x1x"
>>> a.translate({ord('1'):'2', ord('2'):'1'})
'2x11x2x'
>>>
>>> # Just to explain
>>> help(str.translate)
Help on method_descriptor:
translate(...)
S.translate(table) -> str
Return a copy of the string S, where all characters have been mapped
through the given translation table, which must be a mapping of
Unicode ordinals to Unicode ordinals, strings, or None.
Unmapped characters are left untouched. Characters mapped to None
are deleted.
>>>
Note however that I wrote this for Python 3.x. In 2.x, you will need to do this:
>>> from string import maketrans
>>> a = "1x22x1x"
>>> a.translate(maketrans('12', '21'))
'2x11x2x'
>>>
Finally, it is important to remember that the translate method is for interchanging characters with other characters. If you want to interchange substrings, you should use the replace method as Rohit Jain demonstrated.
One way is to use a some temporary string as intermediate replacement:
b = a.replace('1', '#temp_replace#').replace('2', '1').replace('#temp_replace#', '2')
But this may fail, if your string already contains #temp_replace#. This technique is also described in PEP 378
If the "sources" are all one character, you can make a new string:
>>> a = "1x22x1x"
>>> replacements = {"1": "2", "2": "1"}
>>> ''.join(replacements.get(c,c) for c in a)
'2x11x2x'
IOW, make a new string using the get method which accepts a default parameter. somedict.get(c,c) means something like somedict[c] if c in somedict else c, so if the character is in the replacements dictionary you use the associated value otherwise you simply use the character itself.

Convert string into integer

How can I convert string into integer and remove every character from that change.
Example:
S = "--r10-" I want to have this: S = 10
This not work:
S = "--10-"
int(S)
You can use filter(str.isdigit, s) to keep only those characters of s that are digits:
>>> s = "--10-"
>>> int(filter(str.isdigit, s))
10
Note that this might lead to unexpected results for strings that contain multiple numbers
>>> int(filter(str.isdigit, "12 abc 34"))
1234
or negative numbers
>>> int(filter(str.isdigit, "-10"))
10
Edit: To make this work for unicode objects instead of str objects, use
int(filter(unicode.isdigit, u"--10-"))
remove all non digits first like that:
int(''.join(c for c in "abc123def456" if c.isdigit()))
You could just strip off - and r:
int("--r10-".strip('-r'))
use regex replace with /w to replace non word characters with "" empty string. then cast it
I prefer Sven Marnach's answer using filter and isdigit, but if you want you can use regular expressions:
>>> import re
>>> pat = re.compile(r'\d+') # '\d' means digit, '+' means one or more
>>> int(pat.search('--r10-').group(0))
10
If there are multiple integers in the string, it pulls the first one:
>>> int(pat.search('12 abc 34').group(0))
12
If you need to deal with negative numbers use this regex:
>>> pat = re.compile(r'\-{0,1}\d+') # '\-{0,1}' means zero or one dashes
>>> int(pat.search('negative: -8').group(0))
-8
This is simple and does not require you to import any packages.
def _atoi(self, string):
i = 0
for c in string:
i += ord(c)
return i

Categories

Resources