>>> x = "Hello World!"
>>> x[2:]
'llo World!'
>>> x[:2]
'He'
>>> x[:-2]
'Hello Worl'
>>> x[-2:]
'd!'
>>> x[2:-2]
'llo Worl'
Python calls this concept "slicing" and it works on more than just strings. Take a look here for a comprehensive introduction.
Answer from Paolo Bergantino on Stack Overflow>>> x = "Hello World!"
>>> x[2:]
'llo World!'
>>> x[:2]
'He'
>>> x[:-2]
'Hello Worl'
>>> x[-2:]
'd!'
>>> x[2:-2]
'llo Worl'
Python calls this concept "slicing" and it works on more than just strings. Take a look here for a comprehensive introduction.
Just for completeness as nobody else has mentioned it. The third parameter to an array slice is a step. So reversing a string is as simple as:
some_string[::-1]
Or selecting alternate characters would be:
"H-e-l-l-o- -W-o-r-l-d"[::2] # outputs "Hello World"
The ability to step forwards and backwards through the string maintains consistency with being able to array slice from the start or end.
What is the most efficient way to find substrings in strings?
How do I remove a substring from a string by indicating what I am wanting to remove with a string variable?
Videos
Hello,
There are several ways to find substrings in string, You could use substring in string you could use string.index(substring), or you could use string.find(substring) or even use regex.
I'm trying to understand if there is a significant difference between them for my use case, which is finding people names in article titles for example:
I want to check if Leonardo DiCaprio is in Leonardo DiCaprio gets called out for boarding superyacht: ‘eco-hypocrite’
What I usually do is:
def is_substring_in_string(substring: str, string: str):
#same thing as before but on the article title.
string_alphanumeric = ''.join(e for e in string if e.isalnum()).lower()
return substring in string_alphanumeric
def main():
names = ["Harrison Ford","Leonardo DiCaprio","Eddie Murphy","Bruce Willis","Will Smith"]
#removes unwanted charecters such as !@#$%^&*() etc and converts to lower case
names_alnum_lower = [''.join(e for e in x if e.isalnum()).lower() for x in names]
article_title = "Leonardo DiCaprio gets called out for boarding superyacht: eco-hypocrite"
for idx, lower_name in enumerate(names_alnum_lower):
if is_substring_in_string(lower_name,article_title):
print(f"Actor Name: '{names[idx]}' is in article title '{article_title}'")
if __name__ == '__main__':
main()Imagine there are a bunch of articles and people's names. Is this method acceptable or will I be better off using regex or something else?
I have been trying to get this one working all day. I have a string that contains a substring that I would like to remove but the typical replace function isn't working in the way I would like to use it. I have the substring I want to remove stored in a string variable while having the string I am modifying in another variable. I tried to use the function like this
inspected = replace(substring, '')
but for some reason I get an error indicating that only one argument was passed rather than two. I think this function only accepts quotes rather than the variables themselves unless I am doing something wrong. I have been searching for this one all day but to no avail.
example
inspected = "iamthewalrus" substring = "walrus" inspected = replace(substring,"")