How do I turn only the first letter uppercase? [duplicate] - python

This question already has answers here:
Capitalize a string
(9 answers)
Closed 6 years ago.
I have this:
word = raw_input("enter a word")
word[0].upper()
But it still doesn't make the first letter uppercase.

.upper() returns a new string because strings are immutable data types. You ought to set the return value to a variable.
You can use .capitalize over .upper if you want to make only the first letter uppercase.
>>> word = raw_input("enter a word")
>>> word = word.capitalize()
Please note that .capitalize turns the rest of the characters to lowercase. If you don't want it to happen, just go with [0].upper():
word = word[0].upper() + word[1:]

Related

For x in "hello" print a y of underscores [duplicate]

This question already has answers here:
Repeat string to certain length
(15 answers)
Closed 9 months ago.
I am working on a basic project for a stickman game.
My code so far is:
import random
list_of_words = ["Hello"]
word = str(random.choice(list_of_words))
char = int(len(word))
Since I am still working on it, I am only using 1 word instead of many which could complicate things more.
So how this should work is; there is a list of words. One of them gets randomly picked. Then it counts the amount of characters in the word. Lastly, it prints a certain number of underscores depending on the number of characters in the word.
In theory, if correct, it should work like this:
Word = Hello
Number of characters = 5
Result: _____ (5 underscores in one line)
You can remove the call to str() and then generate a string of underscores that has the same length as the original string by using '_' * len(word).
import random
list_of_words = ["Hello"]
word = random.choice(list_of_words)
print('_' * len(word))
This outputs:
_____

Replacing specific word in text with uppercase version of itself [duplicate]

This question already has answers here:
Why doesn't calling a string method (such as .replace or .strip) modify (mutate) the string?
(3 answers)
Closed 3 years ago.
Ok, so this shouldn't be nearly this difficult, but I'm drawing a blank. So, the idea of this program is that it finds words that only occur in a text once, then capitalizes them. Here's the complete code:
from collections import Counter
from string import punctuation
path = input("Path to file: ")
with open(path) as f:
word_counts = Counter(word.strip(punctuation) for line in f for word in line.replace(")", " ").replace("(", " ")
.replace(":", " ").replace("", " ").split())
wordlist = open(path).read().replace("\n", " ").replace(")", " ").replace("(", " ").replace("", " ")
unique = [word for word, count in word_counts.items() if count == 1]
for word in unique:
text = wordlist
text.replace(word, str(word.upper()))
print(text)
It just prints the regular text, with no modifications made.
I know for a fact the first part works, It's just the final for loop thats giving me trouble.
Any idea what I'm screwing up?
Replace this line
text.replace(word, str(word.upper()))
with
text = text.replace(word, str(word.upper()))
string.replace() does not modify the original string instance.
You should assign it back to text.
text = text.replace(word, str(word.upper()))

Using list comprehensions, transform all vowels within a string to upper case? [duplicate]

This question already has answers here:
python how to uppercase some characters in string
(9 answers)
Closed 4 years ago.
Thanks for your help
Example byron = bYrOn
This is an old homework I'm looking over.
It is actually preety simple to turn all letters in a string to upper case!
just use the variable name and follow .upper() at the end e.g.
byron="byron"
byron=byron.upper()
however, because you want to give only certain letters the "uppercase" status, you'd want to first assign all vowels:
vowels = set(['a','e','i','o','u'])
then use some python logic to do the rest:
for char in byron:
if char in vowels:
Word = Word.replace(char,(char.upper()))
print (Word)
if you want to be quick just copy the code below as it is the complete code used to do your task:
x="byron"
vowels = set(['a','e','i','o','u'])
Word=x
for char in Word:
if char in vowels:
Word = Word.replace(char,(char.upper()))
print(Word)

Capitalize first letter ONLY of a string in Python [duplicate]

This question already has answers here:
Capitalize a string
(9 answers)
How can I capitalize the first letter of each word in a string?
(23 answers)
Closed 6 years ago.
I'm trying to write a single line statement that assigns the value of a string to a variable after having ONLY the first letter capitalized, and all other letters left unchanged.
Example, if the string being used were:
myString = 'tHatTimeIAteMyPANTS'
Then the statement should result in another variable such as myString2 equal to:
myString2 = 'THatTimeIAteMyPANTS'
Like this:
myString= myString[:1].upper() + myString[1:]
print myString
Like Barmar said, you can just capitalize the first character and concatenate it with the remainder of the string.
myString = 'tHatTimeIAteMyPANTS'
newString = "%s%s" % (myString[0].upper(), myString[1:])
print(newString) # THatTimeIAteMyPANTS

Trying to delete vowels from a string fails [duplicate]

This question already has answers here:
def anti_vowel - codecademy (python)
(7 answers)
Closed 8 years ago.
What's wrong with this code? The aim is to check wether the entered string contains vowels and delete them
Here is the code:
def anti_vowel(text):
text = str(text)
vowel = "aeiouAEIOU"
for i in text:
for i in vowel.lower():
text = text.replace('i',' ')
for i in vowel.upper():
text = text.replace('i',' ')
return text
It's a lesson on Codecademy
You are trying to replace the string with the value 'i', not the contents of the variable i.
Your code is also very inefficient; you don't need to loop over every character in text; a loop over vowel is enough. Because you already include both upper and lowercase versions, the two loops over the lowercased and uppercased versions are in essence checking for each vowel 4 times.
The following would be enough:
def anti_vowel(text):
text = str(text)
vowel = "aeiouAEIOU"
for i in vowel:
text = text.replace(i,' ')
return text
You are also replacing vowels with spaces, not just deleting them.
The fastest way to delete (rather than replace) all vowels would be to use str.translate():
def anti_vowel(text):
return text.translate(text.maketrans('', '', 'aeiouAEIOU'))
The str.maketrans() static method produces a mapping that'll delete all characters named in the 3rd argument.

Categories

Resources