start_year = int(input("Enter start year:")) end_year = int(input("Enter end year:")) mylist = [] for year in range(start_year,end_year): if ((year % 4 == 0) and (year % 100 != 0)) or (year % 400 == 0): mylist.append(int(year)) for i in range(0, len(mylist), 10): print(*mylist[i:i+10], sep=', ')
- python
- list
- punctuation
eyllanesc
225k18 gold badges130 silver badges198 bronze badges
asked Nov 13, 2019 at 1:06
1
You want something like: 2000, 2004, . ?
Nov 13, 2019 at 1:10
1 Answer
answered Nov 13, 2019 at 1:12
Peter LeimbiglerPeter Leimbigler
10.3k1 gold badge21 silver badges34 bronze badges
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
By default Python‘s print() function ends with a newline. A programmer with C/C++ background may wonder how to print without a newline. 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.
Example 1:
Here, we can end a print statement with any character/string using this parameter.
Python3
print("Welcome to", end = ' ')
print("GeeksforGeeks", end= ' ')
Output:
Welcome to GeeksforGeeksExample 2:
One more program to demonstrate the working of the end parameter.
Python3
print("Python", end='@')
print("GeeksforGeeks")
Output:
Python@GeeksforGeeksExample 3:
The print() function uses the sep parameter to separate the arguments and ends after the last argument.
Python3
print('G','F', sep='', end='')
print('G')
print('09','12','2016', sep='-', end='\n')
print('Red','Green','Blue', sep=',', end='@')
print('geeksforgeeks')
Output
GFG 09-12-2016 Red,Green,Blue@geeksforgeeks
- Dashboard
- Classroom
- Interviews
- Solo Courses
- $0 Coins
Methods in Python
- Course
- Overview
-
Introduction
-
Introduction To Class ●
Welcome to Methods in Python
●Working With The IDE
●Reach Out for Help
-
Introduction To Class ●
- Python For Loops
-
Introduction to For Loops ●
What is a For Loop
●Connection to Iterables
●Syntax and Examples
-
Introduction to For Loops ●
- Python Dictionaries
-
Introduction to Python Dictionaries
●
What is a Python Dictionary
●Python Dictionary Update
●Common uses for Dictionaries
-
Applications of Python Dictionaries
●
Python Dictionary and For Loop
●Python Dictionary Check Membership
-
Introduction to Python Dictionaries
●
-
File Input Output Python
-
Reading and Writing Files in Python ●
Introduction
●Reading a File
●Writing a File
●Line Counter
-
Reading and Writing Files in Python ●
-
Strings in Python
-
Strings ●
Intro
●Iteration
●Lists
●Replace and Strip
●Reversed and Sorted Python
●Join
-
Strings ●
- Certificate
Replace and Strip
Strings
A very useful method is called replace('char_to_replace','replacement'). We use this to get rid of punctuation or any characters that we do not want in our string. For examples, consider the sentence with bad punctuation: Here we want to get rid of the period after hello and the comma after lets. To do this, we will use the replace method as is shown below.
string = 'Hello. World! Lets, get rid of some bad punctuation!'
# Replace the comma
string_1 = string.replace(',','')
# Replace the period
string_2 = string_1.replace('.','')
print(string_2)
This gives back Hello World! Lets get rid of some bad punctuation! Another useful method that we can
use with strings is called .strip(). This will remove specific characters from the end of a string string that we do not want. For example, if you are trying to count the occurrences of words in a string, you want to ignore all the periods and exclamation marks. To do this we use the strip method:
string = 'Hello World! Lets remove the exclamation mark!'
# Replace any exclamation mark or period at the end of the string
string_1 = string.strip('.!')
print(string_1)
This gives back Hello World! Lets remove the exclamation mark. You will notice that the string still has an exclamation mark after the Hello World because the strip method only gets rid of characters
at the end of a string. Both of these methods tend to be very useful for removing html tags from textReplace and Strip
Edit Me on GitHub!