top of page

end=, sep=, +, *, and /

  • John Fan Zhang
  • Jun 6, 2020
  • 2 min read

Python’s print() function comes with a parameter called ‘end’. By default, the value of this parameter is ‘\n’, i.e. the new line character. You can end a print statement with any character/string using this parameter. => https://www.geeksforgeeks.org/gfact-50-python-end-parameter-in-print

print("Welcome to" , end = ' ')

print("GeeksforGeeks", end = ' ')

Output => Welcome to GeeksforGeeks

print("Python" , end = '@')

print("GeeksforGeeks")

Output => Python@GeeksforGeeks

The separator between the arguments to print() function in Python is space by default (softspace feature) -- one space. The ‘sep=’ is used to achieve different types of spaces. Examples: #code for disabling the softspace feature print('G','F','G', sep='') #for formatting a date print('09','12','2016', sep='-') #another example print('pratik','geeksforgeeks', sep='@') Output: GFG 09-12-2016 pratik@geeksforgeeks

The 'sep=' used with 'end=' produces awesome results. print('G','F', sep='', end='') print('G') #\n provides new line after printing the year print('09','12', sep='-', end='-2016\n') print('prtk','agarwal', sep='', end='@') print('geeksforgeeks') Output: GFG 09-12-2016 prtkagarwal@geeksforgeeks

The + (plus) sign, when applied to two strings, becomes a concatenation operator: string + string Examples: fnam = input("May I have your first name, please? ") lnam = input("May I have your last name, please? ") print("Thank you.") print("\nYour name is " + fnam + " " + lnam + ".")

The last line is the same with print("\nYour name is", fnam, lnam + ".", sep=" ")

It is also the same with

print("\nYour name is", end=" ") print(fnam, lnam+".", sep=" ")

The * (asterisk) sign, when applied to a string and number (or a number and string, as it remains commutative in this position) becomes a replication operator: string * number or number * string

For example:

  • "James" * 3 gives "JamesJamesJames"

  • 3 * "an" gives "ananan"

  • 5 * "2" (or "2" * 5) gives "22222" (not 10!)

A number less than or equal to zero produces an empty string.

Try this simple program to "draws" a rectangle

print("+" + 10 * "-" + "+")

print(("|" + " " * 10 + "|\n") * 5, end="")

print("+" + 10 * "-" + "+")

Comentarios


aboutME

I am John Fan Zhang, a data analyst and finance researcher. I hold a PhD in finance, CFA charter and full membership of CFA New Zealand Society. I have fifteen-year experience in corporate investment and eight-year experience in advanced data analysis. My research focuses on the effect of social psychology (culture) on financial decisions. Finance research involves heaps of data analyses that lead me to the data field. I am a Microsoft Certified Solutions Expert (MCSE): Data Management and Analytics (Excel, Power BI, and SQL). Aside from Excel, Power BI and SQL, I am also familiar with econometric tools such as Stata, Eviews, and MATLAB. I use OX and Python for programming. I am an active data community event participant, volunteer, speaker, moderator, program reviewer, including PASS Marathon 2020, Global AI BootCamp Auckland 2019, SQL Saturday Auckland (2017, 2018, 2019), and Definity Conference (2018, 2019, 2020, Auckland, New Zealand).

Auckland, New Zealand

  • Google Site
  • Twitter
  • LinkedIn

©2016 BY JOHN'S DATA STORY

bottom of page