Target and color specific parts of a string

167 Views Asked by At
import colorama
from colorama import Fore, Back, Style
colorama.init()

question_four = "\n4. After you have died, what would you most like people to do when they hear your name? \n"
answers_four = ["ASK for more stories about your adventures", "i DON'T CARE what people think of me after I'm dead, it's what they think of me while I'm alive that counts", "Think with ADMIRATION of your achievements", "MISS you, but smile"]

answers_house_a = ["ask", ...]
answers_house_b = ["don't care", ...]
answers_house_c = ["admiration", ...]
answers_house_d = ["miss", ...]

I would like to color the CAPITALIZED words within the variable answers_four. As I have 20 of those question variables and 20 related answers lists, adding Fore.CYAN + "String" + Fore.RESET to each word I want to color is not an option. Below is what I have managed so far which is printing the answers in cyan but not the specific parts of the strings.

for x in answers_four:
   print(f"{Fore.CYAN}" + x)

Prints (in cyan):

ASK for more stories about your adventures
i DON'T CARE what people think of me after I'm dead, it's what they think of me while I'm alive that counts
Think with ADMIRATION of your achievements
MISS you, but smile
2

There are 2 best solutions below

7
sbottingota On

Your question is a bit hard to understand, but I think what you're asking is for every capitalized word in answer_four to be printed in cyan (and for every other word to be printed normally).

You could do this like this:

import colorama
from colorama import Fore
colorama.init()

answers_four = ["ASK for more stories about your adventures", "i DON'T CARE what people think of me after I'm dead, it's what they think of me while I'm alive that counts", "Think with ADMIRATION of your achievements", "MISS you, but smile"]

for line in answers_four:
    for word in line.split():
        if word.isupper():
            print(Fore.CYAN + word + Fore.RESET, end=" ")

        else:
            print(word, end=" ")

    print()

Output:

ASK for more stories about your adventures 
i DON'T CARE what people think of me after I'm dead, it's what they think of me while I'm alive that counts 
Think with ADMIRATION of your achievements 
MISS you, but smile 

(You can't copy colored text into the question, but it colors the capitalized words cyan.)

You could also do this more concisely, for example like this:

import colorama
from colorama import Fore


colorama.init()

answers_four = ["ASK for more stories about your adventures", "i DON'T CARE what people think of me after I'm dead, it's what they think of me while I'm alive that counts", "Think with ADMIRATION of your achievements", "MISS you, but smile"]
answers_four = "\n".join(answers_four)  # make answers_four into a string, rather than a list

answers_four = " ".join(
    word if not word.isupper()
    else Fore.CYAN + word + Fore.RESET for word in answers_four.split(" ")
    )


print(answers_four)

EDIT

From the OP's comments, I understand that they have a list of lists storing their sentences, and wish for that to be printed:

answers_house_a = ["ask", ...]
answers_house_b = ["don't care", ...]
answers_house_c = ["admiration", ...]
answers_house_d = ["miss", ...]

This could be done in multiple ways, for example:

# add newlines to the end of the lists
answers_house_a = ["ask", ..., "\n"]
answers_house_b = ["don't care", ..., "\n"]
answers_house_c = ["admiration", ..., "\n"]
answers_house_d = ["miss", ..., "\n"]

# store all the words in one list, using list unpacking
answers_four = [*answers_house_a, *answers_house_b, *answers_house_c, *answers_house_d]

# make answers_four into a string, rather than a list
# note that now it is " ".join() not "\n".join() because each item is a word, not a line
answers_four = " ".join(answers_four)  

answers_four = " ".join(
    word if not word.isupper()
    else Fore.CYAN + word + Fore.RESET for word in answers_four.split(" ")
    )

print(answers_four)
0
user16171413 On

I upvoted @sbottingota answer because I believe it resolves the original question. You can also do this with regex by finding the matching patterns and coloring the capitalized words in the list. See it below:

import colorama
from colorama import Fore
import re

colorama.init()

question_four = "\n4. After you have died, what would you most like people to do when they hear your name? \n"
answers_four = ["ASK for more stories about your adventures", "i DON'T CARE what people think of me after I'm dead, it's what they think of me while I'm alive that counts", "Think with ADMIRATION of your achievements", "MISS you, but smile"]

ans_str = ' '.join(answers_four)
re_str = re.findall(r'[a-zA-Z0-9.,\']*', ans_str)
result = [Fore.CYAN + x + Fore.RESET if x.upper() in re_str else x for x in re_str]

print(' '.join(result))

EDIT: Based on additional requests from OP in the comments. My understanding of OP's question is OP wants to know how to target every capitalized word in several answer lists. I did this by combining all the answers list into a single list, looping through and targeting the capitalized words using regex. See the solution below:

import colorama
from colorama import Fore
import re

colorama.init()

answers_house_a = ["ASK for more stories about your adventures"]
answers_house_b = ["i DON'T CARE what people think of me after I'm dead, it's what they think of me while I'm alive that counts"]
answers_house_c = ["Think with ADMIRATION of your achievements"]
answers_house_d = ["MISS you, but smile"]

all_answers_list = [answers_house_a, answers_house_b, answers_house_c, answers_house_d]

for ans in all_answers_list:
    ans_str = ' '.join(ans)
    re_str = re.findall(r'[\w,.?\-\']*', ans_str)
    result = [Fore.CYAN + x + Fore.RESET if x.upper() in re_str else x for x in re_str]
    print(' '.join(result))

You can try adding more lists to all_answers_list or even more words to each list and see that it works.