string-formatting

How to count specific words in each row of a column python pandas

How to count specific words in each row of a column python pandas Question: I have the following dataframe in pandas: test = pd.DataFrame({‘Food’: [‘Apple Cake Apple’, ‘Orange Tomato Cake’, ‘Broccoli Apple Orange’, ‘Cake Orange Cake’, ‘Tomato Apple Orange’], ‘Type’ : [‘Fruit Dessert’, ‘Fruit Veggie’, ‘Veggie Fruit’, ‘Dessert Fruit’, ‘Veggie Fruit’]}) test Food Type 0 …

Total answers: 1

Is there a way to make a forloop identify it's about to end?

Is there a way to make a forloop identify it's about to end? Question: Given this code: columns = 12 num = 0 for num in range(0, columns+1): print(f"{num:>5d}", end=" ") How can I let the for loop know that this is the last time it will run, so that I can add a newline …

Total answers: 1

String Formatting Dollar Sign In Python

String Formatting Dollar Sign In Python Question: So I’m trying to get the dollar sign to appear directly to the left of the digits under the column ‘Cost’. I’m having trouble figuring this out and any help is appreciated. # Question 5 print(“nQuestion 5.”) # Get amount of each type of ticket to be purchased …

Total answers: 3

How to format an int as an uppercase hex string with 0x prefix?

How to format an int as an uppercase hex string with 0x prefix? Question: I have an integer variable: >>> a = id(“string”) and I want to format a to an uppercase hexadecimal string with the prefix 0x. What’s the best way to do this? These are some incorrect outputs: >>> f”{a:#x}” ‘0x115afcae8’ >>> f”{a:#X}” …

Total answers: 1

Why isn't it possible to use backslashes in f-strings?

Why isn't it possible to use backslashes inside the braces of f-strings? How can I work around the problem? Question: In Python >=3.6, f-strings can be used as a replacement for the str.format method. As a simple example, these are equivalent: ‘{} {}’.format(2+2, “hey”) f'{2+2} {“hey”}’ Disregarding format specifiers, I can basically move the positional …

Total answers: 4

ValueError: cannot switch from manual field specification to automatic field numbering

ValueError: cannot switch from manual field specification to automatic field numbering Question: The class: class Book(object): def __init__(self, title, author): self.title = title self.author = author def get_entry(self): return “{0} by {1} on {}”.format(self.title, self.author, self.press) Create an instance of my book from it: In [72]: mybook = Book(‘HTML’,’Lee’) In [75]: mybook.title Out[75]: ‘HTML’ In …

Total answers: 4

Python: String Formatter Align center

Python: String Formatter Align center Question: print(‘%24s’ % “MyString”) # prints right aligned print(‘%-24s’ % “MyString”) # prints left aligned How do I print it in the center? Is there a quick way to do this? I don’t want the text to be in the center of my screen. I want it to be in …

Total answers: 4

How to remove special characters except space from a file in python?

How to remove special characters except space from a file in python? Question: I have a huge corpus of text (line by line) and I want to remove special characters but sustain the space and structure of the string. hello? there A-Z-R_T(,**), world, welcome to python. this **should? the next line#followed- by@ an#other %million^ %%like …

Total answers: 5

f-strings vs str.format()

f-strings vs str.format() Question: I’m using the .format() a lot in my Python 3.5 projects, but I’m afraid that it will be deprecated during the next Python versions because of f-strings, the new kind of string literal. >>> name = "Test" >>> f"My app name is {name}." ‘My app name is Test.’ Does the formatted …

Total answers: 5

Think Python 2nd Edition Exercise 7-1

Think Python 2nd Edition Exercise 7-1 Question: “Square Roots” loop: while True: y = (x+ a/x) / 2 if y == x: return x x = y Copy the loop from “Square Roots” and encapsulate it in a function called mysqrt that takes a as a parameter, chooses a reasonable value of x, and returns …

Total answers: 6