This question already has answers here:
Get the last 4 characters of a string [duplicate]
(2 answers)
Closed 1 year ago.
streetName = "Finley"
print(streetName[???]),
^
|
I'm trying to find out how I can retrieve the last 3 letters of the string using string index. What would I put in the brackets??
print(streetName[-3:]).
This should work.
Related
This question already has answers here:
Given a string how can I remove all the duplicated consecutive letters?
(5 answers)
Closed 1 year ago.
How can i make a basic function with def in python that removes duplicate letters from a string?
Example: input: "abbcdddea"; output: "abcdea"
This code removes duplicates but conserves the order:
string = "abb"
string_without_duplicates = "".join(dict.fromkeys(string))
This question already has answers here:
How do I get a substring of a string in Python? [duplicate]
(16 answers)
Remove final character from string
(5 answers)
Closed 1 year ago.
How can I get the last character of this string?
seed_name = "Cocoa"
As shown in the official Python tutorial,
>>> word = 'Python'
[...]
Indices may also be negative numbers, to start counting from the right:
>>> word[-1] # last character
'n'
This question already has answers here:
How do I get a substring of a string in Python? [duplicate]
(16 answers)
How to move the first letter of a word to the end
(3 answers)
Closed 4 years ago.
Let's say I have an input:
SA3213023215
I want to move the SA to the very end of the input.
How can I do this?
Assuming that SA3213023215 is a string (which input is by default), you could use string slicing:
s = "SA3213023215"
s2 = s[2:] + s[:2]
# yields 3213023215SA
This question already has answers here:
Count number of occurrences of a substring in a string
(36 answers)
Closed 4 years ago.
I want to count the number of times \n appears in string (Student Copy)\nfor\nspecial school\n...Shaping the Future\n408,) before the phrase Shaping the Future. Is there a way to do it without splitting the string?
Output in this case should be 3
You can slice the string up until your substring of interest, and then use count
s = """(Student Copy)\nfor\nspecial school\n...Shaping the Future\n408,)"""
s[:s.index("Shaping the Future")].count('\n')
This question already has answers here:
How do I split a string into a list of characters?
(15 answers)
Closed 5 years ago.
Suppose I get input as apple, how can I split it in list of each character like ['a','p','p','l','e']?
I tried [i for i in input().split('')],
but it outputs error: ValueError: empty separator.
Try list('apple').
This will split the string 'apple' into individual characters and place them inside a list.
Output:
['a','p','p','l','e']