Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3.2 - Concatenate and string formatting behavior not behaving as expected

I want to create a "full file name" variable from several other variables, but the string concatenation and string format operations aren't behaving the way I expect.

My code is below:

file_date = str(input("Enter file date: "))

root_folder = "\\\\SERVER\\FOLDER\\"
file_prefix = "sample_file_"
file_extension = ".txt"

print("")
print("Full file name with concatenation: ")
print(root_folder + file_prefix + file_date + file_extension)
print("Full file name with concatenation, without file_extension: ")
print(root_folder + file_prefix + file_date)
print("")

print("")
print("Full file name with string formatting: ")
print("%s%s%s%s" % (root_folder, file_prefix, file_date, file_extension))
print("Full file name with string formatting, without file_extension: ")
print("%s%s%s" % (root_folder, file_prefix, file_date))
print("")

The output when I run the script is:

C:\Temp>python test.py
Enter file date: QT1

Full file name with concatenation:
.txtRVER\FOLDER\sample_file_QT1
Full file name with concatenation, without file_extension:
\\SERVER\FOLDER\sample_file_QT1


Full file name with string formatting:
.txtRVER\FOLDER\sample_file_QT1
Full file name with string formatting, without file_extension:
\\SERVER\FOLDER\sample_file_QT1

I was expecting it to concatenate the ".txt" at the very end, except it's replacing the first four characters of the string with it instead.

How do I concatenate the extension variable to the end of the string instead of having it replace the first n characters of the string?

In addition to how to solve this particular problem, I'd like to know why I ran into it in the first place. What did I do wrong/what Python 3.2 behavior am I not aware of?

like image 405
tgxiii Avatar asked Jun 06 '11 18:06

tgxiii


People also ask

What does {: 3f mean in Python?

"f" stands for floating point. The integer (here 3) represents the number of decimals after the point. "%. 3f" will print a real number with 3 figures after the point. – Kefeng91.

How do you use %d and %s in Python?

In, Python %s and %d are used for formatting strings. %s acts a placeholder for a string while %d acts as a placeholder for a number. Their associated values are passed in via a tuple using the % operator. This code will print abc 2.

What is %d and %f in Python?

Answer. In Python, string formatters are essentially placeholders that let us pass in different values into some formatted string. The %d formatter is used to input decimal values, or whole numbers. If you provide a float value, it will convert it to a whole number, by truncating the values after the decimal point.

How do you use %s in Python?

The %s operator is put where the string is to be specified. The number of values you want to append to a string should be equivalent to the number specified in parentheses after the % operator at the end of the string value. The following Python code illustrates the way of performing string formatting.


2 Answers

I think the method input used in your example, like so:

file_date = str(input("Enter file date: "))

may be returning a carriage return character at the end.
This causes the cursor to go back to the start of the line when you try to print it out. You may want to trim the return value of input().

like image 67
hopia Avatar answered Oct 19 '22 22:10

hopia


Use this line instead to get rid of the line feed:

file_date = str(input("Enter file date: ")).rstrip()
like image 43
CalMlynarczyk Avatar answered Oct 19 '22 22:10

CalMlynarczyk