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.
Related
This question already has answers here:
Check if multiple strings exist in another string
(17 answers)
Closed 4 years ago.
I want to when I input a string, if words in string is contain in a list of words, delete words in the strings
Or, you could do the following:
raw_string = input("Enter String:")
useless_list = ["Birds", "Cat"]
print(' '.join([i for i in raw_string.split() if i not in useless_list]))
This question already has answers here:
Split string every nth character?
(19 answers)
Closed 4 years ago.
how to split a string into words of 2 letters. Like given string is "HelloThere" now i want to make it ["He","ll","oT","he","re"]. Please help to code that in python.
yourList = []
yourString = "HelloThere"
while yourString:
yourList.append(yourString[:2])
yourString = yourString[2:]
If you print yourList, you will get the result.
This question already has answers here:
How do I reverse a string in Python?
(19 answers)
Closed 5 years ago.
How would I make a word print backwards in Python? For example, if I have this:
word = input("Enter a word: ")
how would I make that print backwards?
The simplest one, using extended slice :
>>> word[::-1]
#driver values :
IN : word = 'abcd'
OUT : 'dcba'
Try This:
a=input()
print ("".join(reversed(a)))
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
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:]