Get the last 4 characters of a string [duplicate] - python

This question already has answers here:
How do I get a substring of a string in Python? [duplicate]
(16 answers)
Closed 7 years ago.
I have the following string: "aaaabbbb"
How can I get the last four characters and store them in a string using Python?

Like this:
>>> mystr = "abcdefghijkl"
>>> mystr[-4:]
'ijkl'
This slices the string's last 4 characters. The -4 starts the range from the string's end. A modified expression with [:-4] removes the same 4 characters from the end of the string:
>>> mystr[:-4]
'abcdefgh'
For more information on slicing see this Stack Overflow answer.

str = "aaaaabbbb"
newstr = str[-4:]
See : http://codepad.org/S3zjnKoD

Related

How to return the last character of a string in Python? [duplicate]

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'

How to delete certain words from string without spaces? [duplicate]

This question already has answers here:
How to remove substring from string in Python 3
(2 answers)
Closed 2 years ago.
Is there I way to delete words from a string in Python if it doesn't have spaces. For example, if you have the string "WUBHELLOWUB" I want to remove "WUB". I tried
s = 'WUBHELLOWUB'
while 'WUB' in s:
ind = s.find('WUB')
s = s[:ind] + s[ind+1:]
print(s)
but it did not work.
You can use regex
import re
data=r"\S*WUB\S*"
re.sub(data, '','WUBWUBHELLO')

Why does count of empty string in Python strings return len(str) + 1? [duplicate]

This question already has answers here:
Why are str.count('') and len(str) giving different output?
(3 answers)
Closed 2 years ago.
The count function of strings return the number of non-overlapping occurrences of substring. However, when I try to count empty string in non-empty or empty string, it does not give 0, but len(str) + 1.
>>> 'aaa'.count('') # it should have been 0
>>> 4
>>> ''.count('') # it should have been 0
>>> 1
What is the logic behind this?
That's because empty strings are considered to exist between all the
characters of a string; for a string length 2, there are 3 empty
strings; one at the start, one between the two characters, and one at
the end.
original answer

how do you check if a string has more than one specific character in python. Example The string, 'mood' would clearly have two 'o' characters [duplicate]

This question already has answers here:
Count the number of occurrences of a character in a string
(26 answers)
Closed 3 years ago.
how do you check if a string has more than one specific character in python. Example The string, 'mood' would clearly have two 'o' characters
You can use the str.count method:
>>> 'mood'.count('o') > 1
True
>>>

How can I split a pipe-separated string into a list? [duplicate]

This question already has answers here:
Split a string by a delimiter in python
(5 answers)
Closed 3 years ago.
I have this string:
var(HELLO,)|var(Hello again)| var(HOW ARE YOU?)|outV(0)|outV(1)|outV(2)|END
I want to split it on the |. I don't want it to split at the white space, only at the |.
Is this possible?
The way to do this is clearly documented here.
Example:
>>> myString = "subString1|substring2|subString3"
>>> myString = myString.split("|")
>>> print myString
["subString1", "subString2", "subString3"]

Categories