Hướng dẫn edabit python

This site is generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join 575,000 other learners and get started learning Python for data science today!

Welcome

Welcome to the LearnPython.org interactive Python tutorial.

Whether you are an experienced programmer or not, this website is intended for everyone who wishes to learn the Python programming language.

You are welcome to join our group on Facebook for questions, discussions and updates.

After you complete the tutorials, you can get certified at LearnX and add your certification to your LinkedIn profile.

Just click on the chapter you wish to begin from, and follow the instructions. Good luck!

Learn the Basics

  • Hello, World!
  • Variables and Types
  • Lists
  • Basic Operators
  • String Formatting
  • Basic String Operations
  • Conditions
  • Loops
  • Functions
  • Classes and Objects
  • Dictionaries
  • Modules and Packages

Data Science Tutorials

  • Numpy Arrays
  • Pandas Basics

Advanced Tutorials

  • Generators
  • List Comprehensions
  • Lambda functions
  • Multiple Function Arguments
  • Regular Expressions
  • Exception Handling
  • Sets
  • Serialization
  • Partial functions
  • Code Introspection
  • Closures
  • Decorators
  • Map, Filter, Reduce

Other Python Tutorials

  • DataCamp has tons of great interactive Python Tutorials covering data manipulation, data visualization, statistics, machine learning, and more
  • Read Python Tutorials and References course from After Hours Programming

Contributing Tutorials

Read more here: Contributing Tutorials

Better use string formatting:

Nội dung chính

  • Use the String Formatting With the Modulo % Sign in Python
  • Use the String Formatting With the str.format() Function in Python
  • Use String Concatenation in Python
  • Use the f-string for String Formatting in Python
  • Use the sep Parameter of the print Statement in Python
  • Related Article - Python Print
  • How do I stop printing with space?
  • How do you print a list without spaces in Python?

print('\n{} you will be {} in ten years.'.format(name, ageinten))

or use sep='', but then you'd have to add trailing and leading spaces to the strings.:

print("\n", name, " you will be ", ageinten, " in ten years.", sep='')

Default value of sep is a space, that's why you're getting a space.

Demo:

>>> name = 'Example'
>>> ageinten =  '20'
>>> print("\n",name," you will be ",ageinten," in ten years.", sep='')

Example you will be 20 in ten years.
>>> print('\n{} you will be {} in ten years.'.format(name, ageinten))

Example you will be 20 in ten years.
  1. Use the String Formatting With the Modulo % Sign in Python
  2. Use the String Formatting With the str.format() Function in Python
  3. Use String Concatenation in Python
  4. Use the f-string for String Formatting in Python
  5. Use the sep Parameter of the print Statement in Python

When we generally use the print statement, we sometimes use a comma (,) as a separator, which sometimes leads to unnecessary spaces between values. Fortunately, you can utilize some alternatives in Python that help you handle these spacing problems.

In this guide, we’ll teach you how to use the different methods to print without spaces between values in Python.

We will take a simple code for all the methods, which takes up a print statement and contains several arguments separated by commas. For example, the following program below uses the comma operator to print the values.

x = 10
print ('The number of mangoes I have are "', x, '"')

Output:

The number of mangoes I have are " 10 "

We should note that there are unnecessary spaces between the number 10 and the double quotes surrounding it. The aim is to prevent or remove this excessive or unnecessary spacing.

Use the String Formatting With the Modulo % Sign in Python

String formatting gives more customization options to the users to go with the classic print statement. The % sign is also known as an interpolation or a string formatting operator.

String formatting can be implemented in two ways, and using the % sign is one of those options.

The % sign, followed by a letter that represents the conversion type, works as a placeholder for the variable. The code below uses the % sign to print without spaces between values in Python.

x = 10
print ('The number of mangoes I have are "%d"' %x )

Output:

The number of mangoes I have are "10"

Use the String Formatting With the str.format() Function in Python

When using the string formatting, braces {} are used to mark the spot in the statement where the variable would be substituted.

The str.format() has been introduced in Python 3 and is available to use in the latest versions of Python. This function is utilized for the efficient handling of complex string formatting.

The following code uses the str.format() function to print without spaces between values in Python.

x = 10
print ('The number of mangoes I have are "{}"'.format(x) )

Output:

The number of mangoes I have are "10"

It is recommended to use the format() function instead of the old % operator in the newer versions of Python.

Use String Concatenation in Python

The + operator, also known as the string concatenation operator, can be used in this case to prevent unnecessary spacing between the values. It’s a direct alternative to comma separation and can be used along with the print statement.

Here is an example code that shows the use of string concatenation in the print statement.

x = 10
print ('The number of mangoes I have are "' + str(x) + '"')

Output:

The number of mangoes I have are "10"

Use the f-string for String Formatting in Python

Python 3.6 introduced the f-string, which is another method of achieving string formatting; however, it has the edge over the two other processes for string formatting mentioned above because it’s comparatively faster than its other two peers.

The following code uses the fstring formatting to print without spaces between values in Python.

x = 10
print (f'The number of mangoes I have are "{x}"')

Output:

The number of mangoes I have are "10"

Use the sep Parameter of the print Statement in Python

You can modify the spacing between the arguments of the print statement by using the sep parameter. The sep parameter can only be found and used in Python 3 and later versions. It can also be utilized for the formatting of the output strings.

The following code uses the sep parameter to print without spaces between values in Python.

x = 10
print ('The number of mangoes I have are "', x, '"', sep='')

Output:

The number of mangoes I have are "10"

Write for us

DelftStack articles are written by software geeks like you. If you also would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Python Print

  • Print Multiple Arguments in Python
  • Print With Column Alignment in Python
  • Print Subscripts to the Console Window in Python
  • Print Quotes in Python
  • How do I stop printing with space?

    The default value of end is "\n" . We are simply changing it to a space or you can also use end="" (no space) to do what printf normally does.

    How do you print a list without spaces in Python?

    To print multiple values or variables without the default single space character in between, use the print() function with the optional separator keyword argument sep and set it to the empty string '' .