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

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

Related

How to display all letters in a string except the first [duplicate]

This question already has answers here:
How do I get a substring of a string in Python? [duplicate]
(16 answers)
Understanding slicing
(38 answers)
Closed 3 years ago.
I want to display an input() string and want to display it so it prints all the letters of the string except the first letter.
string1 = input("enter first string")
string2 = input("enter second string")
print(string1[1] + (string2 - [0]))
I expected it to display as (for examples if string1 was pizza and string 2 was a salad) "palad"
You can use substring. like this:
string1 = input("enter first string")
string2 = input("enter second string")
print(string1[0] + string2[1:])
string2[1:] means: substring from character 2 till the end.

Why is my vowel removal function not working? (Python 2.7) [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.
I'm working through the Python course on codecademy and trying to create a python function that removes vowels in a string and returns the newly modified string.However the function returns the string without any modification (i.e. if I call anti_vowel("abcd") it returns "abcd")
After using a print statement it appears the for loop only runs once, irrespective of the length of the string.
def anti_vowel(string):
for t in string:
if(t.lower()=='a' or t.lower()=='e' or t.lower()=='i' or t.lower()=='u'):
string.replace(t, " ")
print "test"
print string
return string
Strings in Python are immutable, so you will need to make an assignment back to the original string with the replacement on the RHS:
if (t.lower()=='a' or t.lower()=='e' or t.lower()=='i' or t.lower()=='u'):
string = string.replace(t, " ")
But, you could also just re.sub here:
string = re.sub(r'[aeiou]+', '', string, flags=re.IGNORECASE)
You have the return statement inside the for a loop that is why your code is your loop is executing exactly once. Place it outside the loop and your code will work fine.
def anti_vowel(string):
for t in string:
if(t.lower()=='a' or t.lower()=='e' or t.lower()=='i' or t.lower()=='u'):
string.replace(t, " ")
print "test"
print string
return string
For replacing the vowel characters, you cannot replace in the existing variable as strings in python are immutable. You can try this
def anti_vowel(string):
for t in string:
if(t.lower()=='a' or t.lower()=='e' or t.lower()=='i' or t.lower()=='u'):
string=string.replace(t, " ")
print "test"
print string
return string

Replace command condition for replacing strings [duplicate]

This question already has answers here:
Replace all the occurrences of specific words
(4 answers)
Find substring in string but only if whole words?
(8 answers)
Closed 6 years ago.
Want to replace a certain words in a string but keep getting the followinf result:
String: "This is my sentence."
User types in what they want to replace: "is"
User types what they want to replace word with: "was"
New string: "Thwas was my sentence."
How can I make sure it only replaces the word "is" instead of any string of the characters it finds?
Code function:
import string
def replace(word, new_word):
new_file = string.replace(word, new_word[1])
return new_file
Any help is much appreciated, thank you!
using regular expression word boundary:
import re
print(re.sub(r"\bis\b","was","This is my sentence"))
Better than a mere split because works with punctuation as well:
print(re.sub(r"\bis\b","was","This is, of course, my sentence"))
gives:
This was, of course, my sentence
Note: don't skip the r prefix, or your regex would be corrupt: \b would be interpreted as backspace.
A simple but not so all-round solution (as given by Jean-Francios Fabre) without using regular expressions.
' '.join(x if x != word else new_word for x in string.split())

Trying to remove white spaces in python .replace not working [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 6 years ago.
For some reason string.replace(" ", "") is not working.
Is there another way of removing white spaces from a string?
Why is .replace not working in this particular situation?
string = input('enter a string to see if its a palindrome: ')
string.replace(' ', '') # for some reason this is not working
# not sure why program will only work with no spaces
foo = []
bar = []
print(string)
for c in string:
foo.append(c)
bar.append(c)
bar.reverse()
if foo == bar:
print('the sentence you entered is a palindrome')
else:
print('the sentence you entered is not a palindrome')
replace() returns a new string, it doesn't modify the original. Try this instead:
string = string.replace(" ", "")

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

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:]

Categories

Resources