I just started to learn python a few months ago so I'm a newbie here. I was trying to capitalize the first letter of every word in a string. When the input is "hello world" (for example) it works perfectly, but with some inputs like "i love coding" it returns this "I Love CodIng" and it just doesn't make sense to me. Could someone explain to me why this is happening? Here's the code:
def LetterCapitalize(str):
str = str.replace(str[0], str[0].upper())
for i in range(len(str)):
try:
if str[i] == ' ':
str = str.replace(str[i+1], str[i+1].upper())
else:
continue
except IndexError:
break
return str
The str.replace method replaces all occurrences of the given substring in the given main string, so by replacing the letter i with I in i love coding it replaces both is in the string.
Since strings are immutable you can instead convert the given string to a list of characters, so that you can iterate through the list, replace the character if it's either at the start of the string or preceded by a space, and join the list back to a string in the end:
def LetterCapitalize(s):
l = list(s)
for i, c in enumerate(l):
if not i or l[i - 1] == ' ':
l[i] = c.upper()
return ''.join(l)
so that LetterCapitalize('i love coding') returns:
I Love Coding
The problem with the code is your first line,
str = str.replace(str[0], str[0].upper())
Here you replace first letter with its capital, but also you replace that letter in all the following string.
Example :
LetterCapitalize("hello hoher") -> 'Hello HoHer'
You need to modify single character at a given position in the string and not replace all the occurring letters. You should try to work with string as a list. Here is a helpful link : Change one character in a string?
Related
I need to print a string, using this rules:
The first letter should be capital and make all other letters are lowercase. Only the characters a-z A-Z are allowed in the name, any other letters have to be deleted(spaces and tabs are not allowed and use underscores are used instead) and string could not be longer then 80 characters.
It seems to me that it is possible to do it somehow like this:
name = "hello2 sjsjs- skskskSkD"
string = name[0].upper() + name[1:].lower()
lenght = len(string) - 1
answer = ""
for letter in string:
x = letter.isalpha()
if x == False:
answer = string.replace(letter,"")
........
return answer
I think it's better to use a for loop or isalpha () here, but I can't think of a better way to do it. Can someone tell me how to do this?
For one-to-one and one-to-None mappings of characters, you can use the .translate() method of strings. The string module provides lists (strings) of the various types of characters including one for all letters in upper and lowercase (string.ascii_letters) but you could also use your own constant string such as 'abcdef....xyzABC...XYZ'.
import string
def cleanLetters(S):
nonLetters = S.translate(str.maketrans('','',' '+string.ascii_letters))
return S.translate(str.maketrans(' ','_',nonLetters))
Output:
cleanLetters("hello2 sjsjs- skskskSkD")
'hello_sjsjs_skskskSkD'
One method to accomplish this is to use regular expressions (regex) via the built-in re library. This enables the capturing of only the valid characters, and ignoring the rest.
Then, using basic string tools for the replacement and capitalisation, then a slice at the end.
For example:
import re
name = 'hello2 sjsjs- skskskSkD'
trans = str.maketrans({' ': '_', '\t': '_'})
''.join(re.findall('[a-zA-Z\s\t]', name)).translate(trans).capitalize()[:80]
>>> 'Hello_sjsjs_skskskskd'
Strings are immutable, so every time you do string.replace() it needs to iterate over the entire string to find characters to replace, and a new string is created. Instead of doing this, you could simply iterate over the current string and create a new list of characters that are valid. When you're done iterating over the string, use str.join() to join them all.
answer_l = []
for letter in string:
if letter == " " or letter == "\t":
answer_l.append("_") # Replace spaces or tabs with _
elif letter.isalpha():
answer_l.append(letter) # Use alphabet characters as-is
# else do nothing
answer = "".join(answer_l)
With string = 'hello2 sjsjs- skskskSkD', we have answer = 'hello_sjsjs_skskskSkD';
Now you could also write this using a generator expression instead of creating the entire list and then joining it. First, we define a function that returns the letter or "_" for our first two conditions, and an empty string for the else condition
def translate(letter):
if letter == " " or letter == "\t":
return "_"
elif letter.isalpha():
return letter
else:
return ""
Then,
answer = "".join(
translate(letter) for letter in string
)
To enforce the 80-character limit, just take answer[:80]. Because of the way slices work in python, this won't throw an error even when the length of answer is less than 80.
This is the question I was given to solve:
Create a program inputs a phrase (like a famous quotation) and prints all of the words that start with h-z.
I solved the problem, but the first two methods didn't work and I wanted to know why:
#1 string index out of range
quote = input("enter a 1 sentence quote, non-alpha separate words: ")
word = ""
for character in quote:
if character.isalpha():
word += character.upper()
else:
if word[0].lower() >= "h":
print(word)
word = ""
else:
word = ""
I get the IndexError: string index out of range message for any words after "g". Shouldn't the else statement catch it? I don't get why it doesn't, because if I remove the brackets [] from word[0], it works.
#2: last word not printing
quote = input("enter a 1 sentence quote, non-alpha separate words: ")
word = ""
for character in quote:
if character.isalpha():
word += character.upper()
else:
if word.lower() >= "h":
print(word)
word = ""
else:
word = ""
In this example, it works to a degree. It eliminates any words before 'h' and prints words after 'h', but for some reason doesn't print the last word. It doesn't matter what quote i use, it doesn't print the last word even if it's after 'h'. Why is that?
You're calling on word[0]. This accesses the first element of the iterable string word. If word is empty (that is, word == ""), there is no "first element" to access; thus you get an IndexError. If a "word" starts with a non-alphabetic character (e.g. a number or a dash), then this will happen.
The second error you're having, with your second code snippet leaving off the last word, is because of the approach you're using for this problem. It looks like you're trying to walk through the sentence you're given, character by character, and decide whether to print a word after having read through it (which you know because you hit a space character. But this leads to the issue with your second approach, which is that it doesn't print the last string. That's because the last character in your sentence isn't a space - it's just the last letter in the last word. So, your else loop is never executed.
I'd recommend using an entirely different approach, using the method string.split(). This method is built-in to python and will transform one string into a list of smaller strings, split across the character/substring you specify. So if I do
quote = "Hello this is a sentence"
words = quote.split(' ')
print(words)
you'll end up seeing this:
['Hello', 'this', 'is', 'a', 'sentence']
A couple of things to keep in mind on your next approach to this problem:
You need to account for empty words (like if I have two spaces in a row for some reason), and make sure they don't break the script.
You need to account for non-alphanumeric characters like numbers and dashes. You can either ignore them or handle them differently, but you have to have something in place.
You need to make sure that you handle the last word at some point, even if the sentence doesn't end in a space character.
Good luck!
Instead of what you're doing, you can Iterate over each word in the string and count how many of them begin in those letters. Read about the function str.split(), in the parameter you enter the divider, in this case ' ' since you want to count the words, and that returns a list of strings. Iterate over that in the loop and it should work.
How is one of the following versions different from the other?
The following code returns the first letter of a word from string capitalize:
s = ' '.join(i[0].upper() + i[1:] for i in s.split())
The following code prints only the last word with every character separated by space:
for i in s.split():
s=' '.join(i[0].upper()+i[1:]
print s
For completeness and for people who find this question via a search engine, the proper way to capitalize the first letter of every word in a string is to use the title method.
>>> capitalize_me = 'hello stackoverlow, how are you?'
>>> capitalize_me.title()
'Hello Stackoverlow, How Are You?'
for i in s.split():`
At this point i is a word.
s = ' '.join(i[0].upper() + i[1:])
Here, i[0] is the first character of the string, and i[1:] is the rest of the string. This, therefore, is a shortcut for s = ' '.join(capitalized_s). The str.join() method takes as its argument a single iterable. In this case, the iterable is a string, but that makes no difference. For something such as ' '.join("this"), str.join() iterates through each element of the iterable (each character of the string) and puts a space between each one. Result: t h i s There is, however, an easier way to do what you want: s = s.title()
I'm trying to write a script that can take doubled letters (aa or tt, for instance) and change them to that letter followed by ː, the length symbol (aa would become aː, and tt would become tː). I want to do this by iterating through the string, and replacing any character in the string that's the same as the last one with a ː. How do I do that?
You could try something like this. I iterated through string and checked each letter against the previous letter. If they match it performs the replacement if not it moves on and stores the new previous letter in previousletter. Also I used the .lower() method to mactch letters even if one is capitalized and one is not.
string = "Tthis is a testt of the ddouble letters"
previousletter = string[0]
for letter in string:
if letter.lower() == previousletter.lower():
string = string.replace("%s%s" % (previousletter, letter) , "%s:" % (letter))
previousletter = letter
print(string)
And here is the output:
t:his is a test: of the d:ouble let:ers
I hope this helps and feel free to ask any questions on the code that I used. Happy programming!
So the code below properly removes all the vowels from a string as expected.
def disemvowel(string):
# Letters to remove & the new, vowerl free string
vowels_L = list('aeiouAEIOU')
new_string = ""
# Go through each word in the string
for word in string:
# Go through each character in the word
for character in word:
# Skip over vowels, include everything elses
if character in vowels_L:
pass
else:
new_string += character
# Put a space after every word
new_string += ' '
# Exclude space place at end of string
return new_string[:-1]
no_vowels = disemvowel('Nasty Comment: Stack exchange sucks!')
print(no_vowels)
>>>>python remove_vowels.py
>>>>Nsty Cmmnt: Stck xchng scks!
However, when I move the statement: "new_string+= ' '" to where I think it should be (I come from a C/C++ background), I end up getting a weird answer,
def disemvowel(string):
# Letters to remove & the new, vowerl free string
vowels_L = list('aeiouAEIOU')
new_string = ""
# Go through each word in the string
for word in string:
# Go through each character in the word
for character in word:
# Skip over vowels, include everything elses
if character in vowels_L:
pass
else:
new_string += character
# THIS IS THE LINE OF CODE THAT WAS MOVED
# Put a space after every word
new_string += ' '
# Exclude space place at end of string
return new_string[:-1]
no_vowels = disemvowel('Nasty Comment: Stack exchange sucks!')
print(no_vowels)
>>>>python remove_vowels.py
>>>>N s t y C m m n t : S t c k x c h n g s c k s !
Instead of placing a space after a word has finished being iterated over exclusively, a space is also place wherever there was a vowel. I was hoping someone would be able to explain why this occurs, even though in C the result would be quite different. Also, any suggestions to streamline/condense the could would be welcome! : )
for word in string doesn't iterate over the words; it iterates over the characters. You don't need to add spaces at all, because the spaces in the original string are preserved.
As interjay comments, your indentation is way out. Python relies on the indentation to describe which statements belong to what block, instead of the more common BEGIN ... END or { ... }.
In addition, user2357112 observes that you are expecting words from your string, whereas a string is simply a list of characters, and for word in string will set word to one character of string at a time
It is also much cleaner to use not in rather than an if together with a pass.
This is much closer to what you intended
def disemvowel(string):
# Letters to remove & the new, vowel-free string
vowels_list = 'aeiouAEIOU'
new_string = ""
# Go through each character in the string
for character in string:
# Skip over vowels, include everything else
if character not in vowels_list:
new_string += character
return new_string
print disemvowel('Nasty Comment: Stack exchange sucks!')
output
Nsty Cmmnt: Stck xchng scks!