Python change a single character of a string [duplicate] - python

s = 'the brown fox'
...do something here...
s should be:
'The Brown Fox'
What's the easiest way to do this?

The .title() method of a string (either ASCII or Unicode is fine) does this:
>>> "hello world".title()
'Hello World'
>>> u"hello world".title()
u'Hello World'
However, look out for strings with embedded apostrophes, as noted in the docs.
The algorithm uses a simple language-independent definition of a word as groups of consecutive letters. The definition works in many contexts but it means that apostrophes in contractions and possessives form word boundaries, which may not be the desired result:
>>> "they're bill's friends from the UK".title()
"They'Re Bill'S Friends From The Uk"

The .title() method can't work well,
>>> "they're bill's friends from the UK".title()
"They'Re Bill'S Friends From The Uk"
Try string.capwords() method,
import string
string.capwords("they're bill's friends from the UK")
>>>"They're Bill's Friends From The Uk"
From the Python documentation on capwords:
Split the argument into words using str.split(), capitalize each word using str.capitalize(), and join the capitalized words using str.join(). If the optional second argument sep is absent or None, runs of whitespace characters are replaced by a single space and leading and trailing whitespace are removed, otherwise sep is used to split and join the words.

Just because this sort of thing is fun for me, here are two more solutions.
Split into words, initial-cap each word from the split groups, and rejoin. This will change the white space separating the words into a single white space, no matter what it was.
s = 'the brown fox'
lst = [word[0].upper() + word[1:] for word in s.split()]
s = " ".join(lst)
EDIT: I don't remember what I was thinking back when I wrote the above code, but there is no need to build an explicit list; we can use a generator expression to do it in lazy fashion. So here is a better solution:
s = 'the brown fox'
s = ' '.join(word[0].upper() + word[1:] for word in s.split())
Use a regular expression to match the beginning of the string, or white space separating words, plus a single non-whitespace character; use parentheses to mark "match groups". Write a function that takes a match object, and returns the white space match group unchanged and the non-whitespace character match group in upper case. Then use re.sub() to replace the patterns. This one does not have the punctuation problems of the first solution, nor does it redo the white space like my first solution. This one produces the best result.
import re
s = 'the brown fox'
def repl_func(m):
"""process regular expression match groups for word upper-casing problem"""
return m.group(1) + m.group(2).upper()
s = re.sub("(^|\s)(\S)", repl_func, s)
>>> re.sub("(^|\s)(\S)", repl_func, s)
"They're Bill's Friends From The UK"
I'm glad I researched this answer. I had no idea that re.sub() could take a function! You can do nontrivial processing inside re.sub() to produce the final result!

Here is a summary of different ways to do it, and some pitfalls to watch out for
They will work for all these inputs:
"" => ""
"a b c" => "A B C"
"foO baR" => "FoO BaR"
"foo bar" => "Foo Bar"
"foo's bar" => "Foo's Bar"
"foo's1bar" => "Foo's1bar"
"foo 1bar" => "Foo 1bar"
Splitting the sentence into words and capitalizing the first letter then join it back together:
# Be careful with multiple spaces, and empty strings
# for empty words w[0] would cause an index error,
# but with w[:1] we get an empty string as desired
def cap_sentence(s):
return ' '.join(w[:1].upper() + w[1:] for w in s.split(' '))
Without splitting the string, checking blank spaces to find the start of a word
def cap_sentence(s):
return ''.join( (c.upper() if i == 0 or s[i-1] == ' ' else c) for i, c in enumerate(s) )
Or using generators:
# Iterate through each of the characters in the string
# and capitalize the first char and any char after a blank space
from itertools import chain
def cap_sentence(s):
return ''.join( (c.upper() if prev == ' ' else c) for c, prev in zip(s, chain(' ', s)) )
Using regular expressions, from steveha's answer:
# match the beginning of the string or a space, followed by a non-space
import re
def cap_sentence(s):
return re.sub("(^|\s)(\S)", lambda m: m.group(1) + m.group(2).upper(), s)
Now, these are some other answers that were posted, and inputs for which they don't work as expected if we define a word as being the start of the sentence or anything after a blank space:
.title()
return s.title()
# Undesired outputs:
"foO baR" => "Foo Bar"
"foo's bar" => "Foo'S Bar"
"foo's1bar" => "Foo'S1Bar"
"foo 1bar" => "Foo 1Bar"
.capitalize() or .capwords()
return ' '.join(w.capitalize() for w in s.split())
# or
import string
return string.capwords(s)
# Undesired outputs:
"foO baR" => "Foo Bar"
"foo bar" => "Foo Bar"
using ' ' for the split will fix the second output, but not the first
return ' '.join(w.capitalize() for w in s.split(' '))
# or
import string
return string.capwords(s, ' ')
# Undesired outputs:
"foO baR" => "Foo Bar"
.upper()
Be careful with multiple blank spaces, this gets fixed by using ' ' for the split (like shown at the top of the answer)
return ' '.join(w[0].upper() + w[1:] for w in s.split())
# Undesired outputs:
"foo bar" => "Foo Bar"

Why do you complicate your life with joins and for loops when the solution is simple and safe??
Just do this:
string = "the brown fox"
string[0].upper()+string[1:]

Copy-paste-ready version of #jibberia anwser:
def capitalize(line):
return ' '.join(s[:1].upper() + s[1:] for s in line.split(' '))

If only you want the first letter:
>>> 'hello world'.capitalize()
'Hello world'
But to capitalize each word:
>>> 'hello world'.title()
'Hello World'

If str.title() doesn't work for you, do the capitalization yourself.
Split the string into a list of words
Capitalize the first letter of each word
Join the words into a single string
One-liner:
>>> ' '.join([s[0].upper() + s[1:] for s in "they're bill's friends from the UK".split(' ')])
"They're Bill's Friends From The UK"
Clear example:
input = "they're bill's friends from the UK"
words = input.split(' ')
capitalized_words = []
for word in words:
title_case_word = word[0].upper() + word[1:]
capitalized_words.append(title_case_word)
output = ' '.join(capitalized_words)

An empty string will raise an error if you access [1:]. Therefore I would use:
def my_uppercase(title):
if not title:
return ''
return title[0].upper() + title[1:]
to uppercase the first letter only.

Although all the answers are already satisfactory, I'll try to cover the two extra cases along with the all the previous case.
if the spaces are not uniform and you want to maintain the same
string = hello world i am here.
if all the string are not starting from alphabets
string = 1 w 2 r 3g
Here you can use this:
def solve(s):
a = s.split(' ')
for i in range(len(a)):
a[i]= a[i].capitalize()
return ' '.join(a)
This will give you:
output = Hello World I Am Here
output = 1 W 2 R 3g

As Mark pointed out, you should use .title():
"MyAwesomeString".title()
However, if would like to make the first letter uppercase inside a Django template, you could use this:
{{ "MyAwesomeString"|title }}
Or using a variable:
{{ myvar|title }}

The suggested method str.title() does not work in all cases.
For example:
string = "a b 3c"
string.title()
> "A B 3C"
instead of "A B 3c".
I think, it is better to do something like this:
def capitalize_words(string):
words = string.split(" ") # just change the split(" ") method
return ' '.join([word.capitalize() for word in words])
capitalize_words(string)
>'A B 3c'

To capitalize words...
str = "this is string example.... wow!!!";
print "str.title() : ", str.title();
#Gary02127 comment, the below solution works with title with apostrophe
import re
def titlecase(s):
return re.sub(r"[A-Za-z]+('[A-Za-z]+)?", lambda mo: mo.group(0)[0].upper() + mo.group(0)[1:].lower(), s)
text = "He's an engineer, isn't he? SnippetBucket.com "
print(titlecase(text))

You can try this. simple and neat.
def cap_each(string):
list_of_words = string.split(" ")
for word in list_of_words:
list_of_words[list_of_words.index(word)] = word.capitalize()
return " ".join(list_of_words)

Don't overlook the preservation of white space. If you want to process 'fred flinstone' and you get 'Fred Flinstone' instead of 'Fred Flinstone', you've corrupted your white space. Some of the above solutions will lose white space. Here's a solution that's good for Python 2 and 3 and preserves white space.
def propercase(s):
return ''.join(map(''.capitalize, re.split(r'(\s+)', s)))

The .title() method won't work in all test cases, so using .capitalize(), .replace() and .split() together is the best choice to capitalize the first letter of each word.
eg: def caps(y):
k=y.split()
for i in k:
y=y.replace(i,i.capitalize())
return y

You can use title() method to capitalize each word in a string in Python:
string = "this is a test string"
capitalized_string = string.title()
print(capitalized_string)
Output:
This Is A Test String

A quick function worked for Python 3
Python 3.6.9 (default, Nov 7 2019, 10:44:02)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> capitalizeFirtChar = lambda s: s[:1].upper() + s[1:]
>>> print(capitalizeFirtChar('помните своих Предковъ. Сражайся за Правду и Справедливость!'))
Помните своих Предковъ. Сражайся за Правду и Справедливость!
>>> print(capitalizeFirtChar('хай живе вільна Україна! Хай живе Любовь поміж нас.'))
Хай живе вільна Україна! Хай живе Любовь поміж нас.
>>> print(capitalizeFirtChar('faith and Labour make Dreams come true.'))
Faith and Labour make Dreams come true.

Capitalize string with non-uniform spaces
I would like to add to #Amit Gupta's point of non-uniform spaces:
From the original question, we would like to capitalize every word in the string s = 'the brown fox'. What if the string was s = 'the brown fox' with non-uniform spaces.
def solve(s):
# If you want to maintain the spaces in the string, s = 'the brown fox'
# Use s.split(' ') instead of s.split().
# s.split() returns ['the', 'brown', 'fox']
# while s.split(' ') returns ['the', 'brown', '', '', '', '', '', 'fox']
capitalized_word_list = [word.capitalize() for word in s.split(' ')]
return ' '.join(capitalized_word_list)

Easiest solution for your question, it worked in my case:
import string
def solve(s):
return string.capwords(s,' ')
s=input()
res=solve(s)
print(res)

Another oneline solution could be:
" ".join(map(lambda d: d.capitalize(), word.split(' ')))

In case you want to downsize
# Assuming you are opening a new file
with open(input_file) as file:
lines = [x for x in reader(file) if x]
# for loop to parse the file by line
for line in lines:
name = [x.strip().lower() for x in line if x]
print(name) # Check the result

I really like this answer:
Copy-paste-ready version of #jibberia anwser:
def capitalize(line):
return ' '.join([s[0].upper() + s[1:] for s in line.split(' ')])
But some of the lines that I was sending split off some blank '' characters that caused errors when trying to do s[1:]. There is probably a better way to do this, but I had to add in a if len(s)>0, as in
return ' '.join([s[0].upper() + s[1:] for s in line.split(' ') if len(s)>0])

Related

find the uppercase letter on a string and replace it

This is my code :
def cap_space(txt):
e = txt
upper = "WLMFSC"
letters = [each for each in e if each in upper]
a = ''.join(letters)
b = a.lower()
c = txt.replace(a,' '+b)
return c
who i built to find the uppercase latters on a given string and replace it with space and the lowercase of the latter
example input :
print(cap_space('helloWorld!'))
print(cap_space('iLoveMyFriend'))
print(cap_space('iLikeSwimming'))
print(cap_space('takeCare'))
what should output be like :
hello world!
i love my friend
take care
i like swimming
what i get as output instead is :
hello world!
iLoveMyFriend
iLikeSwimming
take care
the problem here is the condition only applied if there only one upper case latter in the given string for some reasons how i could improve it to get it applied to every upper case latter on the given string ?
Being a regex addict, I can offer the following solution which relies on re.findall with an appropriate regex pattern:
def cap_space(txt):
parts = re.findall(r'^[a-z]+|[A-Z][a-z]*[^\w\s]?', txt)
output = ' '.join(parts).lower()
return output
inp = ['helloWorld!', 'iLoveMyFriend', 'iLikeSwimming', 'akeCare']
output = [cap_space(x) for x in inp]
print(inp)
print(output)
This prints:
['helloWorld!', 'iLoveMyFriend', 'iLikeSwimming', 'akeCare']
['hello world!', 'i love my friend', 'i like swimming', 'ake care']
Here is an explanation of the regex pattern used:
^[a-z]+ match an all lowercase word from the very start of the string
| OR
[A-Z] match a leading uppercase letter
[a-z]* followed by zero or more lowercase letters
[^\w\s]? followed by an optional "symbol" (defined here as any non word,
non whitespace character)
You can make use of nice python3 methods str.translate and str.maketrans:
In [281]: def cap_space(txt):
...: upper = "WLMFSC"
...: letters = [each for each in txt if each in upper]
...: d = {i: ' ' + i.lower() for i in letters}
...: return txt.translate(str.maketrans(d))
...:
...:
In [283]: print(cap_space('helloWorld!'))
...: print(cap_space('iLoveMyFriend'))
...: print(cap_space('iLikeSwimming'))
...: print(cap_space('takeCare'))
hello world!
i love my friend
i like swimming
take care
A simple and crude way. It might not be effective but it is easier to understand
def cap_space(sentence):
characters = []
for character in sentence:
if character.islower():
characters.append(character)
else:
characters.append(f' {character.lower()}')
return ''.join(characters)
a is all the matching uppercase letters combined into a single string. When you try to replace them with txt.replace(a, ' '+b), it will only match if all the matchinguppercase letters are consecutive in txt, or there's just a single match. str.replace() matches and replaces the whole seawrch string, not any characters in it.
Combining all the matches into a single string won't work. Just loop through txt, checking each character to see if it matches.
def cap_space(txt):
result = ''
upper = "WLMFSC"
for c in txt:
if c in upper:
result += ' ' + c.lower()
else:
result += c
return result

Better way to remove multiple words from a string?

bannedWord = ["Good", "Bad", "Ugly"]
def RemoveBannedWords(toPrint, database):
statement = toPrint
for x in range(0, len(database)):
if bannedWord[x] in statement:
statement = statement.replace(bannedWord[x] + " ", "")
return statement
toPrint = "Hello Ugly Guy, Good To See You."
print(RemoveBannedWords(toPrint, bannedWord))
The output is Hello Guy, To See You. Knowing Python I feel like there is a better way to implement changing several words in a string. I searched up some similar solutions using dictionaries but it didn't seem to fit this situation.
I use
bannedWord = ['Good','Bad','Ugly']
toPrint = 'Hello Ugly Guy, Good To See You.'
print(' '.join(i for i in toPrint.split() if i not in bannedWord))
Here's a solution with regex:
import re
def RemoveBannedWords(toPrint,database):
statement = toPrint
pattern = re.compile("\\b(Good|Bad|Ugly)\\W", re.I)
return pattern.sub("", toPrint)
toPrint = "Hello Ugly Guy, Good To See You."
print(RemoveBannedWords(toPrint,bannedWord))
Slight variation on Ajay's code, when one of the string is a substring of other in the bannedWord list
bannedWord = ['good', 'bad', 'good guy' 'ugly']
The result of toPrint ='good winter good guy' would be
RemoveBannedWords(toPrint,database = bannedWord) = 'winter good'
as it will remove good first. A sorting is required wrt length of elements in the list.
import re
def RemoveBannedWords(toPrint,database):
statement = toPrint
database_1 = sorted(list(database), key=len)
pattern = re.compile(r"\b(" + "|".join(database_1) + ")\\W", re.I)
return pattern.sub("", toPrint + ' ')[:-1] #added because it skipped last word
toPrint = 'good winter good guy.'
print(RemoveBannedWords(toPrint,bannedWord))
Yet another variation on a theme. If you are going to be calling this a lot, then it is best to compile the regex once to improve the speed:
import re
bannedWord = ['Good', 'Bad', 'Ugly']
re_banned_words = re.compile(r"\b(" + "|".join(bannedWord) + ")\\W", re.I)
def RemoveBannedWords(toPrint):
global re_banned_words
return re_banned_words.sub("", toPrint)
toPrint = 'Hello Ugly Guy, Good To See You.'
print(RemoveBannedWords(toPrint))
As you're checking for the word boundary in the beginning and a non word character at the end, regex is preferable.
Still in-memory array/list can also be used
bannedWord = ['Good', 'Bad', 'Ugly']
toPrint = 'Hello Uglyyy Guy, Good To See You.'
for word in bannedWord:
toPrint = toPrint.replace(word, "")
print(toPrint) 
Hello yy Guy, To See You.
[Program finished]

how to create a list from a string in python 3?

So I am trying to turn a string with multiple separators into a list, but dictated by where the separators are:
ex: ("Hooray! Finally, we're done.", "!,") to be converted to: ['Hooray', ' Finally', " we're done."] based upon the separators given.
As you can see, the string is split into a list based on the separators. My closest attempt:
for ch in separators:
original = ' '.join(original.split(ch))
return(original.split())
when I do this I get the result:
['Hooray', 'Finally', "we're", 'done.']
but I need to have " we're done" as one element of the list, not separated.
I got a suggestion to use a string accumulator, but I don't see how it helps to solve the issue
Thanks
Just do this, using re.split:
>>> import re
>>> original = "Hooray! Finally, we're done."
>>> re.split('!|,', start)
['Hooray', ' Finally', " we're done."]
EDIT:
Without regular expressions, you need a custom function such as:
def multisplit(s, delims):
pos = 0
for i, c in enumerate(s):
if c in delims:
yield s[pos:i]
pos = i + 1
yield list(s[pos:])
And then use it as so:
>>> original = "Hooray! Finally, we're done."
>>> multisplit(original, '!,')
['Hooray', ' Finally', " we're done."]
You can use re.split with an appropriate expression:
>>> data=("Hooray! Finally, we're done.", "!,")
>>> re.split("[%s]" % re.escape(data[1]), data[0])
['Hooray', ' Finally', " we're done."]
Splits the first element of the tuple by every character in the tuples second element.
The regex is made out of the tuple's second string where all regex special charcters are properly escaped. [some chars] means that every character inside the Square Brackets will be a seperator.
A simple solutoin without regular expressions could be:
def split_on_separators(word, separators):
word_list = [word]
auxList = []
for sep in separators:
for w in word_list:
auxList.extend(w.split(sep))
word_list = auxList
auxList = list()
return word_list
example = "Hooray! Finally, we're done."
separators = '!,'
split_on_separators(example, separators)
Out[49]: ['Hooray', ' Finally', " we're done."]
Change the string so all the separators are the same, then split on that one:
def separate(words, separators):
sep0 = separators[0]
for sep in separators[1:]:
words = words.replace(sep, sep0)
return words.split(sep0)

Substring search for multiword strings - Python

I want to check a set of sentences and see whether some seed words occurs in the sentences. but i want to avoid using for seed in line because that would have say that a seed word ring would have appeared in a doc with the word bring.
I also want to check whether multiword expressions (MWE) like word with spaces appears in the document.
I've tried this but this is uber slow, is there a faster way of doing this?
seed = ['words with spaces', 'words', 'foo', 'bar',
'bar bar', 'foo foo foo bar', 'ring']
docs = ['these are words with spaces but the drinks are the bar is also good',
'another sentence at the foo bar is here',
'then a bar bar black sheep,
'but i dont want this sentence because there is just nothing that matches my list',
'i forgot to bring my telephone but this sentence shouldn't be in the seeded docs too']
docs_seed = []
for d in docs:
toAdd = False
for s in seeds:
if " " in s:
if s in d:
toAdd = True
if s in d.split(" "):
toAdd = True
if toAdd == True:
docs_seed.append((s,d))
break
print docs_seed
The desired output should be this:
[('words with spaces','these are words with spaces but the drinks are the bar is also good')
('foo','another sentence at the foo bar is here'),
('bar', 'then a bar bar black sheep')]
Consider using a regular expression:
import re
pattern = re.compile(r'\b(?:' + '|'.join(re.escape(s) for s in seed) + r')\b')
pattern.findall(line)
\b matches the start or end of a "word" (sequence of word characters).
Example:
>>> for line in docs:
... print pattern.findall(line)
...
['words with spaces', 'bar']
['foo', 'bar']
['bar', 'bar']
[]
[]
This should work and be somewhat faster than your current approach:
docs_seed = []
for d in docs:
for s in seed:
pos = d.find(s)
if not pos == -1 and (d[pos - 1] == " "
and (d[pos + len(s)] == " " or pos + len(s) == len(d))):
docs_seed.append((s, d))
break
find gives us the position of the seed value in the doc (or -1 if it is not found), we then check that the characters before and after the value are spaces (or the string ends after the substring). This also fixes the bug in your original code that multiword expressions don't need to start or end on a word boundary - your original code would match "words with spaces" for an input like "swords with spaces".

Determining if a query is in a string

I have a list of query terms, each with a boolean operator associated with them, like, say:
tom OR jerry OR desperate AND dan OR mickey AND mouse
Okay, now I have a string containing user-defined input, inputStr.
My question is, in Python, is there a way to determine if the string defined by the user contains the words in the "query"?
I have tried this:
if ('tom' or 'jerry' or 'desperate' and 'dan' or 'mickey' and 'mouse') in "cartoon dan character desperate":
print "in string"
But it doesn't give the output i expect.
As you can see, I don't care about whether the query terms are ordered; just whether they are in the string or not.
Can this be done? Am I missing something like a library which can help me achieve the required functionality?
Many thanks for any help.
To check whether any of the words in a list are in the string:
any(word in string for word in lst)
Example:
# construct list from the query by removing 'OR', 'AND'
query = "tom OR jerry OR desperate AND dan OR mickey AND mouse"
lst = [term for term in query.split() if term not in ["OR", "AND"]]
string = "cartoon dan character desperate"
print any(word in string for word in lst)
If you use re.search() as #jro suggested then don't forget to escape words to avoid collisions with the regex syntax:
import re
m = re.search("|".join(map(re.escape, lst)), string)
if m:
print "some word from the list is in the string"
The above code assumes that query has no meaning other than the words it contains. If it does then assuming that 'AND' binds stronger than 'OR' i.e., 'a or b and c' means 'a or (b and c)' you could check whether a string satisfies query:
def query_in_string(query, string):
for term in query.split('OR'):
lst = map(str.strip, term.split('AND'))
if all(word in string for word in lst):
return True
return False
The above could be written more concisely but it might be less readable:
def query_in_string(query, string):
return any(all(word.strip() in string for word in term.split('AND'))
for term in query.split('OR'))
Example
query = "tom OR jerry AND dan"
print query_in_string(query, "cartoon jerry") # -> False no dan or tom
print query_in_string(query, "tom is happy") # -> True tom
print query_in_string(query, "dan likes jerry") # -> True jerry and dan
If you want to reject partial matches e.g., 'dan' should not match 'danial' then instead of word in string you
could use re.search() and add '\b':
re.search(r"\b%s\b" % re.escape(word), string)
I would use a regular expression:
>>> import re
>>> s = "cartoon dan character desperate"
>>> l = ['dan', 'mickey', 'mouse']
>>> print re.search('(%s)' % '|'.join(l), s)
<_sre.SRE_Match object at 0x0233AA60>
>>> l = ['nothing']
>>> print re.search('(%s)' % '|'.join(l), s)
None
Where s is the string to search in and l is a list of words that should be in s. If the search function doesn't return None, you have a match.
if ('tom' or 'jerry' or 'desperate' and 'dan' or 'mickey' and 'mouse') in "cartoon dan character desperate"
does not mean what you think it means, because the parentheses cause the or and and operations to be evaluated first, e.g:
>>> "tom" or "jerry" or "desperate" and "dan" or "mickey" and "mouse"
'tom'
... so your if-clause really means if 'tom' in "cartoon dan character desperate".
What you probably meant was something like:
if ('tom' in inputStr) or ('jerry' in inputStr) or ('desperate' in inputStr and 'dan' in inputStr) or ('mickey' in inputStr and 'mouse' in inputStr)

Categories