18-10-2024 Morning
18-10-2024 Morning
----------------------------------------
What is a String?
String is a group of characters (alphabets both uppercase & lowercase, digits, and
special characters)
enclosed within quotations.
Example:
# Single Quoted String
s1 = 'Python 3.12'
print("Type =", type(s1))
print("Value =", s1)
print()
s4 = """ Python
Rules! """
print("Type =", type(s4))
print("Value =", s4)
print()
-----------------------------------------------------------------------------------
-------------------------------------------------------------------------
2. Strings are Immutable -> cannot be modified
Example:
s = "C++"
print("String =" , s)
print("Address of String =", id(s))
s = "Java"
print("String =" , s)
print("Address of String =", id(s))
Output:
String = C++
Address of String = 2657202361120
String = Java
Address of String = 2657162934384
Example:
s = "java"
print("String =" , s)
print("Address of String =", id(s))
print()
Output:
String = java
Address of String = 2413110471360
Example:
# Reading the Input from the user
s = input("Enter the String: ")
# while Loop
print("while Loop")
i = 0
while i < len(s):
print(i,s[i])
i+=1
print()
-----------------------------------------------------------------------------------
-------------------------------------------------------------------------
4. Data is stored internally based on indexes
NOTE: 3 types in Python are Index Based: Lists, Strings and Tuples
Example:
s = "welcome Anu!"
# Invalid Index will generate Error - IndexError: string index out of range
print(s[100])
print(s[-20])
-----------------------------------------------------------------------------------
-------------------------------------------------------------------------
5. Slicing can be applied
Syntax:
String [start: stop: step]
Example:
s = "welcome Anu!"
# Every 3rd character from the String: Forward direction - start to end
print(s[::3])
s = "PYTHON PROGRAMMING"
print(s[::-2])
print(s[2:3])
print(s[2:3:-1])
print(s[-6:7:-2])
print(s[4:-8])
print(s[9:-10])
print(s[22::-6])
print(s[2:-22:-1])
print(s[7::-1])
-----------------------------------------------------------------------------------
-------------------------------------------------------------------------
6. Operator '+' does the work of concatenation / joining
Example:
s1 = "Welcome"
s2 = "to"
s3 = "Python"
Output:
Concatenated String: Welcome to Python
-----------------------------------------------------------------------------------
-------------------------------------------------------------------------
7. Operator '*' does the work of repetition
Example:
s = "Welcome"
newStr = s * 3
print("Repeated String:" , newStr)
Output:
Repeated String: WelcomeWelcomeWelcome
-----------------------------------------------------------------------------------
-------------------------------------------------------------------------
8. Membership Operators: in, not in
Used for the purpose of searching whether the given sub-string exists within the
specified string or not.
Boolean value is returned as the output.
• in: Returns True if a character exists within the given string otherwise False.
• not in: Returns True if a character does not exist in the given string otherwise
returns False.
Example:
str = "Program"
Output:
Is 'gram' not present in the String?: False
Is 'grams' not present in the String?: True
NOTE:
The same Job can also be done using the repr() function. The difference is that the
output will be within single quotation.
Example:
print(repr("I \nLove\tPython"))
Output:
'I \nLove \tPython'
-----------------------------------------------------------------------------------
-------------------------------------------------------------------------
Built-In String Functions
1. len()
2. upper()
3. lower()
4. capitalize()
5. center()
6. count()
7. startswith()
8. endswith()
9. find()
10. rfind()
11. index()
12. rindex()
13. title()
14. join()
15. ljust()
16. rjust()
17. swapecase()
18. lstrip()
19. rstrip()
20. strip()
21. replace()
22. split()
23. rsplit()
24. max()
25. min()
NOTE: All the functions starting with 'is' are Boolean - result will be either True
(or) False
26. isalpha()
27. isdigit()
28. isnumeric()
29. isdecimal()
30. isidentifier()
31. isprintable()
32. isspace()
33. istitle()
34. isupper()
35. islower()
Note:
Difference between find() and index() Functions
Difference between title() and capitalize() Functions
-----------------------------------------------------------------------------------
-------------------------------------------------------------------------