DEV Community

no longer crazy
no longer crazy

Posted on

Python Journey: Day 1 - Exploring Print, Input, Variables, and String Manipulation

Welcome to Day 1 of our Python journey! Today, we dive into the basics of Python programming and explore fundamental concepts such as the print function, input function, variables, and string manipulation. By the end of this blog, you'll have a solid understanding of these concepts and even complete a project to swap two variables. Let's get started!

  1. The Print Function: The print function is an essential tool in Python that allows us to display information on the screen. It's as simple as using the print keyword followed by parentheses containing the text or variables you want to display. Let's take a look at an example:
print("My name is Ifedayo")
Enter fullscreen mode Exit fullscreen mode

This code snippet will output the text "My name is Ifedayo" to the console.

  1. The Input Function: The input function enables us to receive input from the user during program execution. It prompts the user with a message and waits for them to provide input. The input function stores the user's input as a string. Here's an example:
name = input("Please enter your name: ")
print("Hello, " + name + "! Nice to meet you.")
Enter fullscreen mode Exit fullscreen mode

In this code snippet, the input function prompts the user to enter their name. The entered name is stored in the variable name, which is then used in the print statement to greet the user.

  1. Variables: Variables are containers used to store data in Python. They can hold different types of values, such as numbers, strings, or even more complex data structures. Let's see an example:
message = "Hello, world!"
print(message)
Enter fullscreen mode Exit fullscreen mode

In this example, we assign the string "Hello, world!" to the variable message. Then, we use the print function to display the value of the variable, which is "Hello, world!" in this case.

  1. String Manipulation: Python provides various ways to manipulate strings, such as concatenation, slicing, and formatting. One simple string manipulation technique is joining two strings together using the "+" operator. Let's look at an example:
first_name = "John"
last_name = "Doe"
full_name = first_name + " " + last_name
print(full_name)
Enter fullscreen mode Exit fullscreen mode

In this code snippet, we create two variables first_name and last_name to store the respective names. Then, we concatenate the two variables with a space in between to create the full_name variable. Finally, we print the value of full_name, which will be "John Doe" in this case.

Project: Swapping Two Variables:
As a beginner exercise, we can create a small project to swap the values of two variables. Here's an example implementation:

a = 5
b = 10

print("Before swapping:")
print("a =", a)
print("b =", b)

# Swapping the values
temp = a
a = b
b = temp

print("After swapping:")
print("a =", a)
print("b =", b)
Enter fullscreen mode Exit fullscreen mode

In this project, we have two variables a and b with initial values. By using a temporary variable temp, we can swap the values of a and b. After the swap, we print the values of a and b to verify the successful swap.

Conclusion:
Congratulations on completing Day 1 of our Python journey! We explored the print function, input function, variables, and string manipulation. You've learned how to display information, receive user input, store data in variables, manipulate strings, and even completed a project

Top comments (0)