What is the source of these spaces and what is the best way to eliminate them?

Just starting out as a beginner in Python. I am currently learning the language through a book called "Breaking Ciphers with Python". As part of a basic encryption program, I have written the following code snippet (this is just the beginning):

import pyperclip

fg = lambda text, color: "\33[38;5;" + str(color) + "m" + text + "\33[0m"
bg = lambda text, color: "\33[48;5;" + str(color) + "m" + text + "\33[0m"

# Simple usage: print(fg("text", 160))

msg = input('\n\nEnter your secret:\n\n')

msg_len_int = len(msg)

msg_len_str = str(len(msg))

print('\n\nChoose key (must be less than', fg((msg_len_str), 40), '): ')

key = input('\n\nChoose your here: ')

When executed, the output looks like this:

https://i.stack.imgur.com/AsB4I.png

I'm puzzled by the presence of two space characters that I did not include - one before and one after fg((msg_len_str), 40).

Answer №1

The official Python documentation states:

All positional arguments are converted to strings similar to the behavior of str() function and written to the output, separated by the defined value of sep and concluded with end.

If you want to eliminate any extra spaces, simply assign an empty string to the sep parameter like this:

print('\n\nEnter preferred key (should be under', convert_length_to_string((msg_len_str), 40), '): ', sep='')

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

I require assistance merging strings that include numerical values

As I begin my scripting course today, I am faced with the challenge of combining strings that contain numbers without actually adding them together in Python. I understand that number1 + number2 will add the numbers, but I am unsure how to simply combine ...

Slowly scrolling down using Selenium

Struggling with performing dynamic web scraping on a javascript-rendered webpage using Python. 1) Encountering an issue where elements load only when scrolling down the page slowly. Tried methods such as: driver.execute_script("window.scrollTo(0, Y)") ...

Ordering Django objects by their creation date within the last week

models.py class short_url(models.Model): """ This is a short_url class """ blocked = models.BooleanField(default=False) updated_at = models.DateTimeField(auto_now=True) ...

Having trouble with Kivy installation: encountering Cython/GCC error

After following the steps on the official website to install Kivy, I encountered some issues: $ sudo apt-get install python-setuptools python-pygame python-opengl \ python-gst0.10 python-enchant gstreamer0.10-plugins-good python-dev \ build- ...

What is the best way to switch out a substring and add a new one in Python?

This code is functional but may not be the most efficient way to replace a substring with another substring that has been previously modified. Input string : text = ["part1 Pirates (2006)", "part2 Pirates (2006)" ] Output stri ...

The attempted decoding of a JSON object was unsuccessful. The provided JSON is not valid

I've encountered an unusual issue In my program, I am sending a JSON string through a socket: json_string = JSONEncoder().encode({ "id_movil": str(id_movil), "correo": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfema ...

Is there a way to display this date as Day - Month - Year format?

def ModifyDate(BadDate): Day = BadDate[5:] Month = BadDate[2:6] Year = BadDate[:2] return Day + "-" + Month + "-" + Year print(ModifyDate("202011Jan")) I'm having trouble with the output (1Jan-2011-20). I'm not an expert, so any ...

Interconnected memory within a group of processes

I am facing an issue while attempting to share memory among 4 processes. Unfortunately, I have been unable to resolve this problem on my own and seek assistance. Please review the logic flow: from multiprocessing import Pool, Value from time import sleep ...

An HTTPError occurred while using the Bing REST API, with the given request details and response code, message, headers, and

Is there a way to retrieve latitude and longitude locations for a large number of addresses without getting blocked by services like Bing? I have around 100k+ addresses that I need to process, but it seems like my current code is causing me to be flagged ...

Automate selecting an option from a dropdown using Python with Selenium

I am encountering an issue while trying to choose an option from The error message I'm receiving is: selenium.common.exceptions.ElementNotVisibleException: Message: element not interactable: Element is not currently visible and may not be manipula ...

Is there a way to extract the text from a textarea using webdriver?

When attempting to retrieve the contents of a textarea within an HTML form using webdriver in Python, I am able to get the text, but the newlines are missing. Despite consulting the selenium docs, which provide limited information: class selenium.webdrive ...

Having difficulty retrieving the ID from a Kivy method

After the user logs in, I'm attempting to create a card with a scroll view on the ViewDecks screen. To achieve this, I invoked the ViewDecks.ShowDecks() method from within the login class. However, when running the code, I encounter an error message: ...

Error: It seems that the 'psql' program is not installed on your system or cannot be found in your path

Currently on a Windows system without virtualenv, psycopg2 is installed through Pip along with the latest version of PostgreSQL. Upon executing ./ manage.py dbshell, the following error is encountered: CommandError: You seem to be missing the 'psql& ...

Python 2: Exploring the differences between a Unicode object and a string object

I came across this issue on StackOverflow, but I was unable to find a satisfactory solution: The problem I am facing involves comparing Unicode strings received from a server with hardcoded strings in my code. Even though I understand why a simple == comp ...

Tips for extracting data from a webpage that requires clicking the pagination button to load the content

I am currently using Python BeautifulSoup to scrape data from a website. The website has pagination, and everything is working smoothly until I reach page 201. Strangely enough, when I try to access page 201 directly through the URL in the browser, it retu ...

Please indicate the number of cores in the `n_jobs` parameter

Within Sklearn, the n_jobs parameter is utilized in various functions to specify the number of cores to be used. This allows users to dictate the amount of processing power allocated for a specific task; for instance, inputting 1 uses one core while -1 s ...

Select the list item containing the desired text by clicking on it

When trying to click on a specific element based on the text displayed on this page using Python 3 and Chrome driver, I encountered an issue. For instance, when searching for "BEBES", I used the following code: WebDriverWait(browser, 10).until(EC.element_ ...

Running code within the __init__ function

When working on my class project, I encountered a problem while trying to untar xz/bx2/gz files within the init section. The code snippet I am using for this task is as follows: class myClass(object): def __init__(self, *args): for i in args: ...

pursuing the team (without achieving the expected components)

My attempt to scrape data from a website using Selenium is facing an issue. Despite removing the text that contains "team," Selenium fails to detect this element. Why is it not grabbing all the elements following "team"? Is there a better way or a method l ...

Problem encountered with color() function in turtle module

Can you help me troubleshoot an error I keep encountering in my program? I'm trying to allow the user to specify the color of lines but it's not working as expected. import turtle wn = turtle.Screen() alex = turtle.Turtle sides = int(input("Ent ...