# variable- Assigning single value to variable
first_name = "Mounika"
last_name = "Kavuru"
Age = 32
print (first_name)
print (last_name)
print (Age)
print("Full Name : " +(first_name) +" " +(last_name))
print("Age : " +str(Age)) # casting integer to string to avoid datatype
error
# variable - Assigning multiple values to variable
Math_Score, Phy_Score, Che_Score = 75, 65,70
Mounika = savni = 260
print("Maths :" +str(Math_Score))
print("Physics :"+str(Phy_Score))
print("Chemistry :" +str(Che_Score))
print("Mounika total score : " +str(Mounika))
print("Sanvi total score :" +str(savni))
# string Methods
#creating a string
str1 = "tommy"
str2 = "gimmy"
print(str1 + " " + "and" +" " +str2) # Single string and string
concatination
S = """Working on string Methods"""
print(S)
s = '''Multi line strings '''
print(s)
print(len(s)) # output: 13
print(S[5]) # Accessing 5th character using indexing the string
print(s[7]) # Accessing 7th character using indexing the string
print(S[-2]) # Accessing character using negative index
# String Slicing
print(s[1:4]) # Retrieves characters from index 1 to 3:
print(s[:3]) # Retrieves characters from beginning to index 2:
print(s[3:]) # Retrieves characters from index 3 to the end:
print(s[::-1]) # Reverse a string
print([Link]()) # Removes spaces from both ends
print([Link]("line", "lines")) # Replaces line with lines
print([Link]()) # output: In upper case
print([Link]()) # output: In lower case
print(s * 3) # Repeating string
print("Geeks" in s) # The in keyword checks if a particular substring
is present in a string.
print("Multi" in s) # The in keyword checks if a particular substring
is present in a string.
a = 2
b = '3.77'
c = -8
T = '{0:.4f} {0:3d} {2} {1}'.format(a, b, c)
print(T)
# String Immutability concept
# name_1 = "Aarun"
# name_1[0] = 'T'
# Output: TypeError: 'str' object does not support item assignment
# We cannot update the string after declaring it means once an
immutable the objects instantiated, its value cannot be changed
# lower(): Converts all characters in the string to lowercase.
# capitalize(): Capitalizes the first character of the string and makes
the rest lowercase.
# title(): Capitalizes the first letter of each word in the string.
# swapcase(): Swap the cases of all characters in a string
Python - Modify Strings
1. string modification by changing the its case
2. Replacing Substrings
3. Trimming Whitespace
4. Concatenating Strings
5. Slicing Strings
6. Checking for Substrings
7. Formatting Strings
string modification by changing the its case
lower(): Converts all characters in the string to lowercase.
upper() : Method which converts all characters in the string to uppercase.
capitalize(): Capitalizes the first character of the string and makes the rest lowercase.
title(): Capitalizes the first letter of each word in the string.
swapcase(): Swap the cases of all characters in a string
Example:
s = "hello world"
print([Link]())
print([Link]())
print([Link]())
Replacing substrings
We can replace parts of a string using replace() method. This method takes two
arguments: 1) old substring we want to replace and 2) new substring we want to
replace it with.
Example:
s1 = "I love Python"
s2 = [Link]("Python", "programming")
print(s2)
Trimming Whitespace
Sometimes, strings may have unwanted spaces at the beginning or end. To remove
these, we can use the strip() method. This method removes any leading and trailing
whitespace from the string.
Example:
s = " Hello World! "
print([Link]())
Concatenating Strings
Concatenating strings means joining two or more strings together. In Python, we can
do this using the + operator.
We can also use join() when concatenating a list of strings.
Example:
s1 = "Hello"
s2 = "World"
print(s1 + " " + s2)
Slicing Strings
String slicing allows you to extract a portion of a string. We can specify a starting
and ending index and Python will return a substring. We can also use the step
parameter to skip characters.
Example:
s = "Hello, Python!"
print(s[0:5]) # Output: "Hello" (characters from index 0 to 4)
print(s[7:]) # Output: "Python!" (characters from index 7 to the end)
print(s[:5]) # Output: "Hello" (characters from the start to index 4)
print(s[::2]) # Output: "Hoo yhn" (every second character)
Checking for a substring
To check if a string contains a certain substring, we can use in keyword.
Example:
s = "Python is fun"
print("Python" in s)
Formatting Strings
Python allows we to format strings in a readable way using f-strings
(formatted string literals), which were introduced in Python 3.6. We can
insert variables or expressions inside a string using curly braces {}.
Example
s = "Geek"
print(f"name - {s}")