Python Basic Concepts
Do You Know?
1)In Python we can get to know, how many number of characters are there in a string?
What is a string =In Python, a string is a sequence of characters enclosed in either single quotes ('
) or double quotes ("
). Strings are used to represent text and can contain letters, numbers, symbols, and whitespace.
So to count that we can write the code
name = input("Enter your name : ")
:- This line asks you to type your name and hit Enter. Whatever you type gets saved in a variable called
name
.
- This line asks you to type your name and hit Enter. Whatever you type gets saved in a variable called
len(name)
:- This part counts how many letters are in the name you entered.
print("Length of your name is:", len(name))
:- This line shows a message on the screen that says how many letters are in your name.
So, if you typed "Alice," the program would tell you, "Length of your name is: 5" because "Alice" has 5 letters.
- The code asks the user for their name.It then calculates how many characters are in the entered name.Finally, it prints the length of the name.
2)How can you use Python to count the occurrences of a specific character in a given string?
First, you create a variable and assign a sentence to it. Next, you use a method to count how many times a specific character, like "a"
, appears in that sentence. Finally, you print the count to see how many times the character occurs. This way, you can find out the frequency of any character in a given string.
4o mini
Python Variables
Variables are containers for storing data values
Variable is a reference to an object stored in a memory
whenever I want to refer or write a variable we will not use double quotes (")
name = "John"
print(name)
output = John
In programming execution is from top to bottom
In python variable can be assigned a new Variable.This can even change the variables type,due to dyanically type.
A variable name must start with a letter or the underscore character
A variable name cannot start with a number
A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
Variable names are case-sensitive (age, Age and AGE are three different variables)
A variable name cannot be any of the Python keywords.
Conventions = Use clear names,Add underscore to separate two letters, avoid single letters.
- use snake case = user_name
maintain consistency
ASSIGNING VALUES TO MULTIPLE VARIABLES
f- string
num1 = 10
print(f"My number is {num1}")
output = My number is 10
1
name = input("Enter your name : ")
print("Length of your name is:",len(name))
2
str = "hey users,I am glad to say that you are going to be Successful in life "
print(str.count("a"))
#ans = 5