This question already has answers here:
How to get the size of a string in Python?
(6 answers)
Closed 7 years ago.
(New to python and stack overflow)
I was curious if there was a way to count the amount of letters in a string for python. for example:
string="hello"
I just want something to count the letters then output it into a variable for later use.
The following will give the length of a string:
len(string)
In your case, you can assign it:
numLetters = len(string)
This function can be used for other objects besides strings. For additional uses, read the documentation.
Use python function len, i.e.:
size = len(string)
len()
https://docs.python.org/2/library/functions.html#len
DEMO
https://ideone.com/mhpdLi
Related
This question already has answers here:
Count occurrences of a substring in a list of strings
(5 answers)
How do I count the occurrences of a list item?
(29 answers)
Closed 2 months ago.
I'm checking a string against a large list of strings (approx. 400k+). It seems to be running far quicker than I thought it would to check against these. As such, I want to count how many strings are being iterated through to check if it is functioning properly or not, checking against the full list before confirming a non-match.
My function operates like so:
if any(string_to_check in string for string in list_of_strings):
print("String Matched:" + string_to_check)
return True
else:
print("String Not Matched:" + string_to_check)
return False
How could I add to this, to see how many strings in the list_of_strings are iterated through before matching or not matching?
Thanks in advance.
This question already has answers here:
str.startswith with a list of strings to test for
(3 answers)
Closed 1 year ago.
I've tried using the or function to input multiple words for the same output, but it only takes the first word as the input and not the rest. How do I solve this? Thanks!
For instance:
message.content.startswith("hi" or "hey")
only takes in "hi" as an input and not "hey".
I've tried adding the words in to a list and it doesn't work as well. I'm relatively new to coding so i'm sorry in advance if it's a stupid question
You can code like this:
message.content.startswith(("hi", "hey"))
From the Python documentation for str.startswith(prefix[, start[, end]]), I've added emphasis:
Return True if string starts with the prefix, otherwise return
False. prefix can also be a tuple of prefixes to look for. With
optional start, test string beginning at that position. With optional
end, stop comparing string at that position.
This question already has answers here:
How to use a variable inside a regular expression?
(12 answers)
Closed 2 years ago.
I need find a repeated sequences of same my_string = 'ABCD' in string my_data = 'ABCDABCDRRRABCD' and return the longest one 'ABCDABCD'.
If I use result = ([max(i) for i in re.findall(r'((ABCD)\2+)', data)]) It's works fine, but I can't put a variable my_string so I can use it for difference strings in some loop.
To put variable inside of regular expression you can use .format
result = ([max(i) for i in re.findall(r'(({0})\2+)'.format(my_string), my_data)])
This question already has answers here:
Python string count not working properly? [duplicate]
(2 answers)
Closed 4 years ago.
when executing the command in python
str="BANANA"
print(str.count("ANA"))
give answer as 1.
but real answer would be 2. how to solve this
It's true that this might be misleading most tutorial sites say that count returns the number of occurences in the string, it should actually say it returns the nnumber of non-overlapping occurences in the string
This question already has answers here:
How do I get a substring of a string in Python? [duplicate]
(16 answers)
Closed 9 years ago.
I have a list of strings in the format: 'foo7bar'. How is it possible in Python to remove the 7 along with any characters that follow?
This is similar to this question, but I need the answer for Python.
You can do this using Python's slice notation:
>>> mystr = 'foo7bar'
>>> mystr[:mystr.index('7')]
'foo'
>>>
The format for slice notation is [start:stop:step]. The index method of a string finds the position of the first occurrence of its input.
Note however that if you are dealing with something more complex (such as matching patterns), you might want to look into Regular Expressions. For this operation though, the slice notation is sufficient.