Question

In: Computer Science

Python Programming: Using re library, and a given string recipe1 return the indications after each ingredient...

Python Programming:

Using re library, and a given string recipe1 return the indications after each ingredient '@': for example if recipe1 = "@turkey baked @350degrees" print an output like: [@turkey, @350degrees]

Solutions

Expert Solution

Program:

import re
recipe1 = input()
print(re.findall(r'@[A-Za-z0-9]+',recipe1))

Explaination:

Line 1: import re

It imports the re module into your python module. re module contains the functions that supports regular expressions.

Line 2: recipe1 = input()

It takes the user input and stores it in the variable called recipe1

Line 3: print(re.findall(r'@[A-Za-z0-9]+',recipe1))

It prints the result.

re.findall(regex,string,flags) is a function which accepts maximum of 3 arguments. It returns all the substrings in a string which matches the provided regular expression.
regex: regular expression

string: the string on which the operations will be performed

flags: the flags (default: 0)

Here, "r" means raw string.

Explaination of regular expression:

@[A-Za-z0-9]+

This regular expression will first search for a "@" symbol in the string, after that it checks for digits or character with the symbol.


Related Solutions

Python please A string is one of most powerful data types in programming. A string object...
Python please A string is one of most powerful data types in programming. A string object is a sequence of characters and because it is a sequence, it is indexable, using index numbers starting with 0. Similar to a list object, a string object allows for the use of negative index with -1 representing the index of the last character in the sequence. Accessing a string object with an invalid index will result in IndexError exception. In Python a string...
C Programming Language Please Given a string S and keyword string K, encrypt S using keyword...
C Programming Language Please Given a string S and keyword string K, encrypt S using keyword Cipher algorithm. Note: The encryption only works on alphabets. Numbers and symbols such as 1 to 9, -, &, $ etc remain unencrypted. Input:     Zombie Here     secret     where: First line represents the unencrypted string S. Second line represents the keyword string K. Output:     ZLJEFT DTOT Explanation: We used "secret" keyword there. Plain Text: A B C D E F G H I J K...
Using python: Given a string x, write a program to check if its first character is...
Using python: Given a string x, write a program to check if its first character is the same as its last character. If yes, the code should output "True"
Given the nested collection that maps each term to a set of strings   Return a string...
Given the nested collection that maps each term to a set of strings   Return a string of terms that are repeated in all the nested sets Given : {apple=[apple BALL carrot, ball !carrot! ,!Dog*&]} {apple=[apple BALL carrot, ball !carrot! ,!Dog*&], dog=[ball !carrot! ,!Dog*&]} Return: [ball !carrot! ,!Dog*&] Public static String common(Map<String, Set<Sting>> map) { }
double_vowels Given a string, return a copy of the string with all of the vowels doubled....
double_vowels Given a string, return a copy of the string with all of the vowels doubled. Consider the five letters 'aeiou' as vowels. double_vowels('pencil') → 'peenciil' double_vowels('xyzzy') → 'xyzzy' double_vowels('catalog') → 'caataaloog' middle_hash Given a string, do three hash marks '###' appear in the middle of the string? To define middle, we'll say that the number of chars to the left and right of the '###' must differ by at most one. middle_hash('aa###bb') → True middle_hash('aaa###bb') → True middle_hash('a###bbb') →...
Python 3.7: 1. Write a generator expression that repeats each character in a given string 4...
Python 3.7: 1. Write a generator expression that repeats each character in a given string 4 times. i.e. given “gone”, every time you call the next method on the generator expression it will display “gggg”, then “oooo”, … etc and instantiate it to an object called G.  G = (…..) # generatorExpression object 2. Test and verify that iter(G) and G are the same things. What does this tell you about the nature of a Generator object/expression? 3. Assign Iter(G) to...
Write a small program to encrypt and decrypt a message using Python library.
Write a small program to encrypt and decrypt a message using Python library.
Python: If I am given a string consisting only of parentheses - (, ), {, },...
Python: If I am given a string consisting only of parentheses - (, ), {, }, [, and ], how would I write a Boolean function that takes a string and decides if it consists of valid nested parenthesis? One of the hints given was to use stack. For example --> {()[{}]}' is valid.
IN PYTHON Given a string with duplicate characters in it. Write a program to generate a...
IN PYTHON Given a string with duplicate characters in it. Write a program to generate a list that only contains the duplicate characters. In other words, your new list should contain the characters which appear more than once. Suggested Approach Used two nested for loops. The first for loop iterates from 0 to range(len(input_str)). The second for loop iterates from first_loop_index + 1 to range(len(input_str)). The reason you want to start at first_loop_index + 1 in the nested inner loop...
Please write code in c++ using iostream library. Also you can use any string library. Create...
Please write code in c++ using iostream library. Also you can use any string library. Create structure plane with the following: 1)plane's ID[int type] 2) location[char*] 3) departure time[char*] Your task is to find the plane with the smallest departure time for the selected city. Pointer, dynamic array and structures have to be used in this code. Input: NOTE: It's just an example of input, you should write code generally for any inputs. First line contains n(0 < n <...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT