Skip to content

Updated the code of format_strings.py and updated readme - (Solves #34) #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions 3 - Comments/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,12 @@ Comments start with a hash character (#) and allow you to document your code.
Comments are ignored when code is executed.

- [Comments](https://docs.python.org/3/reference/lexical_analysis.html?highlight=comment)

“”” Docstrings ”””

The docstrings are declared using “””triple double quotes”””.
It gives programmers an easy way of adding quick notes with every Python module, function, class, and method.
Note: The strings defined using triple-quotation mark are docstring in Python. However, it might appear to you as a regular comment.
As long as the string is not assigned to a variable, Python will read the code, but then ignore it, and you have made a multiline comment.

- [Docstrings](https://www.python.org/dev/peps/pep-0257/)
7 changes: 7 additions & 0 deletions 3 - Comments/comments_are_not_executed.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,10 @@
# print('Hello world')
# print("Hello world")
# No output will be displayed!
"""
This is a docstring
written in
more than just one line.
It can be used as a multiline comment.
"""

10 changes: 10 additions & 0 deletions 3 - Comments/docstrings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def my_function():
"""Do nothing, but document it.

This is a function docstring.
"""

pass
#In order to print or access the docstrings use __doc__.
print(my_function.__doc__)

1 change: 1 addition & 0 deletions 4 - String variables/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Python can store and manipulate strings. Strings can be enclosed in single or double quotes. There are a number of string methods you can use to manipulate and work with strings

- [strings](https://docs.python.org/3/tutorial/introduction.html#strings)
- [format strings](https://docs.python.org/3.4/library/string.html#string-formatting)
- [string methods](https://docs.python.org/3/library/stdtypes.html#string-methods)

Converting to string values
Expand Down
19 changes: 15 additions & 4 deletions 4 - String variables/format_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,18 @@
first_name = input('What is your first name? ')
last_name = input('What is your last name? ')

# the capitalize function will return the string with
# the first letter uppercase and the rest of the word lowercase
print ('Hello ' + first_name.capitalize() + ' ' \
+ last_name.capitalize())
# Custom string formatting
# Using the + operator to concatenate strings
print ('Hello ' + first_name + ' ' + last_name)

# Using place holders to print strings
print ('Hello, {} {} '.format(first_name, last_name)

# Using index numbers {0} and {1} to be sure the arguments are placed in the correct placeholders
print ('Hello {0} {1}'.format(first_name, last_name)

# Reversing the index numbers to change the positions of placeholders
print ('Hello {1} {0}'.format(first_name, last_name)

# Only available in Python 3
print (f'Hello, {first_name} {last_name}')