Manipulating strings in python - concentrating on part of a user's input - python

resp = raw_input("What is your favorite fruit?\n")
if "I like" in resp:
print "%s" - "I like" + " is a delicious fruit." % resp
else:
print "Bubbles and beans."
OK I know this code doesn't work, and I know why. You can't subtract strings from each other like numbers.
But is there a way to break apart a string and only use part of the response?
And by "is there a way" I really mean "how," because anything is possible in programming. :D
I'm trying to write my first chatterbot from scratch.

One option would be to simply replace the part that you want to remove with an empty string:
resp = raw_input("What is your favorite fruit?\n")
if "I like" in resp:
print "%s is a delicious fruit." % (resp.replace("I like ", ""))
else:
print "Bubbles and beans."
If you want to look into more advanced pattern matching to grab out more specific parts of strings via flexible patterns, you might want to look into regular expressions.

# python
"I like turtles".replace("I like ","")
'turtles'

Here's a way to do it with regular expressions:
In [1]: import re
In [2]: pat = r'(I like )*(\w+)( is a delicious fruit)*'
In [3]: print re.match(pat, 'I like apples').groups()[1]
apples
In [4]: print re.match(pat, 'apple is a delicious fruit').groups()[1]
apple

Can you just strip "I like"?
resp.strip("I like")
be careful of case sensitivity though.

Related

How to give a space between 'apple' and 'is ' in a code written in python?

I have coded Apple is the favorite fruit of SP in python.
But in the output, I got: Appleis the favorite fruit of SP
Plz, help me to identify the mistake and tell me how to add a space between Apple and is.
The code is:
hey = "hello world"
print(hey)
name = "sp"
food = "apple"
print(str(food) + "is the favourite fruit of " + str(name))
You can also use "formatted string literals" (a.k.a. "f-strings", available since Python 3.6) to make it easier on yourself:
>>> f"{food} is the favourite fruit of {name}"
apple is the favourite fruit of sp
Just like what the other answers suggest, you can use f-strings to join things together.
However, if you want to use the same method as in your question, you an do this:
hey = "hello world"
print(hey)
name = "sp"
food = "apple"
print(str(food) + " is the favourite fruit of " + str(name))
The difference here is that there is a space before the word is,
so instead of being "is the favourite..." it becomes " is the favourite...".
This will add a space just after str(food) and so there will be a space in between apple and is.
Hope this was helpful :-)
2 easy ways, first your food could be "apple ", or in your string, insert a space before + "is..., so " is....." In either case, you need to add a space.
You can use format printing here,
hey = "Hello world"
print(hey)
name = "sp"
food = "apple"
print(f"{food} is the favourite food of the {name}")
output
Hello world
apple is the favourite food of the sp
The simplest answer for you is adding a space after the double quotes before the "s".
hey = "hello world"
print(hey)
name = "sp"
food = "apple"
print(str(food) + " is the favourite fruit of " + str(name))
The next step would be using commas instead of the "+" symbols to which a space is automatically made, and you won't feel like you have to define the data type.
print(food, "is the favourite fruit of",name)
The best answer that will be most useful in most situations is using an f-string where you type "f" before your string and then place curly brackets "{}" with your variables enclosed. I think you'll find this to be more comfortable and natural.
print(f'{food} is the favourite fruit of {name}')

Specific pattern strings from List in python 3

Requirement:using regex want to fetch only specific strings i.e. string betwee "-" and "*" symbols from input list. Below is the code snippet
ZTon = ['one-- and preferably only one --obvious', " Hello World", 'Now is better than never.', 'Although never is often better than *right* now.']
ZTon = [ line.strip() for line in ZTon]
print (ZTon)
r = re.compile(".^--")
portion = list(filter(r.match, ZTon)) # Read Note
print (portion)
Expected response:
['and preferably only one','right']
Using regex
import re
ZTon = ['one-- and preferably only one --obvious', " Hello World", 'Now is better than never.', 'Although never is often better than *right* now.']
pattern=r'(--|\*)(.*)\1'
l=[]
for line in ZTon:
s=re.search(pattern,line)
if s:l.append(s.group(2).strip())
print (l)
# ['and preferably only one', 'right']
import re
ZTon = ['one-- and preferably only one --obvious', " Hello World", 'Now is better than never.', 'Although never is often better than *right* now.']
def gen(lst):
for s in lst:
s = ''.join(i.strip() for g in re.findall(r'(?:-([^-]+)-)|(?:\*([^*]+)\*)', s) for i in g)
if s:
yield s
print(list(gen(ZTon)))
Prints:
['and preferably only one', 'right']

Parsing file name with RegEx - Python

I'm trying to get the "real" name of a movie from its name when you download it.
So for instance, I have
Star.Wars.Episode.4.A.New.Hope.1977.1080p.BrRip.x264.BOKUTOX.YIFY
and would like to get
Star Wars Episode 4 A New Hope
So I'm using this regex:
.*?\d{1}?[ .a-zA-Z]*
which works fine, but only for a movie with a number, as in 'Iron Man 3' for example.
I'd like to be able to get movies like 'Interstellar' from
Interstellar.2014.1080p.BluRay.H264.AAC-RARBG
and I currently get
Interstellar 2
I tried several ways, and spent quite a lot of time on it already, but figured it wouldn't hurt asking you guys if you had any suggestion/idea/tip on how to do it...
Thanks a lot!
Given your examples and assuming you always download in 1080p (or know that field's value):
x = 'Interstellar.2014.1080p.BluRay.H264.AAC-RARBG'
y = x.split('.')
print " ".join(y[:y.index('1080p')-1])
Forget the regex (for now anyway!) and work with the fixed field layout. Find a field you know (1080p) and remove the information you don't want (the year). Recombine the results and you get "Interstellar" and "Star Wars Episode 4 A New Hope".
The following regex would work (assuming the format is something like moviename.year.1080p.anything or moviename.year.720p.anything:
.*(?=.\d{4}.*\d{3,}p)
Regex example (try the unit tests to see the regex in action)
Explanation:
\.(?=.*?(?:19|20)\d{2}\b)|(?:19|20)\d{2}\b.*$
Try this with re.sub.See demo.
https://regex101.com/r/hR7tH4/10
import re
p = re.compile(r'\.(?=.*?(?:19|20)\d{2}\b)|(?:19|20)\d{2}\b.*$', re.MULTILINE)
test_str = "Star.Wars.Episode.4.A.New.Hope.1977.1080p.BrRip.x264.BOKUTOX.YIFY\nInterstellar.2014.1080p.BluRay.H264.AAC-RARBG\nIron Man 3"
subst = " "
result = re.sub(p, subst, test_str)
Assuming, there is always a four-digit-year, or a four-digit-resolution notation within the movie's file name, a simple solution replaces the not-wanted parts as this:
"(?:\.|\d{4,4}.+$)"
by a blank, strip()'ing them afterwards ...
For example:
test1 = "Star.Wars.Episode.4.A.New.Hope.1977.1080p.BrRip.x264.BOKUTOX.YIFY"
test2 = "Interstellar.2014.1080p.BluRay.H264.AAC-RARBG"
res1 = re.sub(r"(?:\.|\d{4,4}.+$)",' ',test1).strip()
res2 = re.sub(r"(?:\.|\d{4,4}.+$)",' ',test2).strip()
print(res1, res2, sep='\n')
>>> Star Wars Episode 4 A New Hope
>>> Interstellar

Asking multiple things in an if statement?

im new to StackOverflow and Python, I am working on my first program that is not hello world, it is a simple rock, paper, scissors game.
So, my question is can you ask multiple things in an if statement?
quick sample
sport = raw_input("Whats a good sport?")
if sport == 'Football','Soccer','Hockey':
print 'Those are fun sports!'
elif sport != 'Football','Soccer','Hockey':
print 'I dont like those sports!'
I know there are ways to fix that code but I am curious as to if that is a thing?
You can use and and or:
if sport == "Fooball" or sport == "Soccer":
print "those are fun sports!"
You can also check for a string in a list (or in the following example, a tuple) of strings:
if sport in ("Football", "Soccer", "Hockey"):
print "those are fun sports"
You could also use regular expressions to find if the sport is fun or not. The benefit to regular expressions is that it makes it easy to handle different scenarios. For example if the user inputted football instead of Football you could still determine that the sport is fun or not.
import re
pattern = re.compile('[fF]ootball|[sS]occer|[hH]ockey')
sport = 'Football'
if pattern.search("Football"):
print sport + " is fun"
else:
print sport + " is not fun"
For more info:
https://docs.python.org/3.5/howto/regex.htmlYou

How to return a word in a string if it starts with a certain character? (Python)

I'm building a reddit bot for practice that converts US dollars into other commonly used currencies, and I've managed to get the conversion part working fine, but now I'm a bit stuck trying to pass the characters that directly follow a dollar sign to the converter.
This is sort of how I want it to work:
def run_bot():
subreddit = r.get_subreddit("randomsubreddit")
comments = subreddit.get_comments(limit=25)
for comment in comments:
comment_text = comment.body
#If comment contains a string that starts with '$'
# Pass the rest of the 'word' to a variable
So for example, if it were going over a comment like this:
"I bought a boat for $5000 and it's awesome"
It would assign '5000' to a variable that I would then put through my converter
What would be the best way to do this?
(Hopefully that's enough information to go off, but if people are confused I'll add more)
You could use re.findall function.
>>> import re
>>> re.findall(r'\$(\d+)', "I bought a boat for $5000 and it's awesome")
['5000']
>>> re.findall(r'\$(\d+(?:\.\d+)?)', "I bought two boats for $5000 $5000.45")
['5000', '5000.45']
OR
>>> s = "I bought a boat for $5000 and it's awesome"
>>> [i[1:] for i in s.split() if i.startswith('$')]
['5000']
If you dealing with prices as in float number, you can use this:
import re
s = "I bought a boat for $5000 and it's awesome"
matches = re.findall("\$(\d*\.\d+|\d+)", s)
print(matches) # ['5000']
s2 = "I bought a boat for $5000.52 and it's awesome"
matches = re.findall("\$(\d*\.\d+|\d+)", s2)
print(matches) # ['5000.52']

Categories