How to use str.startswith with multiple strings? [duplicate] - python

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.

Related

Function requires a path. How do I make it into a raw path? [duplicate]

This question already has answers here:
Convert regular Python string to raw string
(12 answers)
Closed 1 year ago.
I wrote a function in Python that takes a file path as an argument. Ideally, I would like to 'concatenate' an r at the beginning to escape the characters, and turn it into r"C:\User\name\location".
I am having trouble finding any solutions- are there any modules to help with this?
You do not require any modifications to the function at all.
def f(path):
...
...
f(r"C:\User\name\location")
The "r" you referred to would be used to form the string that you pass to the function. A string is a string, it does not matter how you form it, but Python offers you different ways of doing so e.g.:
f("C:\\User\\name\\location")
By the time the function is passed the string, the string has already been formed. It now makes no difference how it was formed, only that it has all of the correct characters in all the correct places!

Python 3.x replace() with index [duplicate]

This question already has answers here:
Changing one character in a string
(15 answers)
Closed 2 years ago.
A question I came up with:
I'm trying to write a function that replaces the first and the fourth letter of a word with the same letter just capitalized.
Currently I am working with the string.replace() method. It works great for most of the time, except when there is an equal letter to the one on the fourth place before it.
Example: "bananas"
What I would expect the program to do is to return "BanAnas" but for a reason it return "BAnanas". If I use the word "submarine" it would just work fine, "SubMarine".
The code I wrote is this:
def old_macdonald(name):
name = name.replace(name[0], name[0].upper(), 1)
name = name.replace(name[3], name[3].upper(), 1)
return name
Can someone explain why this is happening?
It's because name.replace(name[3], name[3].upper(), 1) looks for the first character matching name[3]. Stop using replace altogether, chop up your string by slicing.

Alternative Ways to Reverse a String [duplicate]

This question already has answers here:
How do I reverse a string in Python?
(19 answers)
Closed 5 years ago.
While doing a homework question, forgetting that that there is a built-in reverse function for strings, I came up with my own way of reversing strings.
So here it is:
for i in range(len(string)):
reversed = string[i] + reversed
I was wondering if this is an efficient (say, if I have a really long string) and correct way of reversing.
You could compare the timing. It's probably quite inefficient, because you make a new string object on every loop iteration and you loop through each character in the string in a Python loop. The built-in function however uses native C code (CPython).
There's a one-liner: reversed = string[::-1]
However, it's kind of hard to read unless you already know the syntax. So you could always bury it in a function with a more helpful name:
def reverse(string):
return string[::-1]

Count letters in string python [duplicate]

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

Python Function To Find String Between Two Markers [duplicate]

This question already has answers here:
How to extract the substring between two markers?
(22 answers)
Closed 4 years ago.
I'm looking to build a string function to extract the string contents between two markers. It returns an extraction list
def extract(raw_string, start_marker, end_marker):
... function ...
return extraction_list
I know this can be done using regex but is this fast? This will be called billions of times in my process. What is the fastest way to do this?
What happens if the markers are the same and appear and odd number of times?
The function should return multiple strings if the start and end markers appear more than once.
You probably can't go faster than:
def extract(raw_string, start_marker, end_marker):
start = raw_string.index(start_marker) + len(start_marker)
end = raw_string.index(end_marker, start)
return raw_string[start:end]
But if you want to try regex, just try to benchmark it. There's a good timeit module for it.

Categories