-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefault.py
More file actions
138 lines (105 loc) · 7.11 KB
/
Copy pathdefault.py
File metadata and controls
138 lines (105 loc) · 7.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# different types of escape sequence
# 1. single quote - \'
print('it\'s alright')
print("it\"s alright")
print("chandan said \"hi\" to darshan")
# 2. carriage return
print("python\rJava")
# 3. backspace
print("hello\bworld")
# different string methods
# 1. capitalize() - make the first character of the entire string uppercase and make everything else lowercase
a = "welcome home!!"
print(a.capitalize())
b = '''this is apple.
this is pineapple'''
print(b.capitalize())
print("DGGTD".capitalize())
# 2. centre() - places a string in centre of a given width by adding spaces or another character around it.
text = "hello"
print(text.center(11)) #hello has 5 characters, 11-5 = 6 so 3 space right and 3 space left, if in case we give 12 instead of 11, spaces become 7 then 3 space on left and 4 space of right
print(text.center(11,"*"))
print("ASDFG".center(12,'@'))
# 3. count() - The count() method returns the number of times a specified value appears in the string.
print("i love apple, apple are my fav. apple are sweet".count("s"))
print("i love apple, apple are my fav. apple are sweet".count("apple"))
print("banana".count("a",2,6))
# 4. endswith() - it is a python string method that checks whether a string ends with a particular character or word. it returns true or false.
print("hello.".endswith("."))
print("hello.3".endswith("3",0,3))
print("hello.3".endswith("."))
# 5. find() - in Python is a string method used to find the position (index) where a specified substring starts inside a string.
print("hi python world".find("python"))
print("hello python".find("java")) #find() returns -1 when the specified substring is not found in the string.
print("banana".find("a",2))
print("banana".find("a",2,3))
# 6. format() - in Python is used to insert values into a string at specified positions/placeholders.
print("my name is {},my age is {}.".format("shravani",19)) #basic positional arguments
print("my name is {1},my age is {0}.".format(19,"shravani")) #indexed argumnets
print("my name is {name},my age is {age}.".format(name = "shravani",age = 19)) #keyword arguments
# 7. index() - In Python, the .index() method is identical to .find() in searching for a substring and accepting optional start and end arguments, but differs by raising a "ValueError" if the substring is missing instead of returning -1.
print("hello python".index("python"))
'''Why use .index() instead of .find()?
You use .index() when the presence of the substring is mandatory for your code to work. If it's missing, you want the program to stop and throw an error immediately rather than silently failing and passing -1 into the rest of your code.'''
print("banana".index("a",2))
# 8. isalnum() method checks whether all characters in a string are alphanumeric (meaning they are either letters or numbers) and returns a boolean (True or False).
print("ndnsk38".isalnum())
print("2".isalnum())
print("hi hello".isalnum())
print("résumé".isalnum()) #Unicode letters like é are accepted
print("éñüçåприветγειαمرحبا".isalnum())
print("½".isalnum()) #Unicode fraction characters are also considered
print("²".isalnum())
print("Ⅷ".isalnum())
# 9. isalpha() method checks whether all characters in a string are alphabetic (meaning only letters from the alphabet, with no numbers, spaces, or symbols) and returns a boolean (True or False).
print("hgsins ".isalpha())
print("résumé".isalnum())
print("éñüçåприветγειαمرحبا".isalnum()) #Unicode letters like é are accepted
# 10. isascii() - .isascii() method checks whether all characters in a string are ASCII characters (meaning they fall within the standard range of code points from 0 to 127, covering standard English letters, numbers, and basic punctuation) and returns a boolean (True or False).
print("hxb4!!".isascii())
print("résumé".isascii())
# 11. isdecimal() - .isdecimal() method checks whether all characters in a string are base-10 decimal characters (digits 0 through 9), returning a boolean (True or False).
print("233".isdecimal())
print("٠١٢٣٤".isdecimal()) # Output: True (Arabic-Indic decimal digits are also recognized)
print("123.456".isdecimal())
# 12. .isdigit() method checks whether all characters in a string are digits and returns a boolean (True or False).
print("1223".isdigit())
print("²".isdigit()) # Superscript 2 is considered a digit
# 13. isidentifier() method checks whether a string is a valid identifier (meaning it can legally be used as a variable name, function name, or class name in Python code), returning a boolean (True or False).
print("1asgg_12".isidentifier())
print("if_12".isidentifier())
# 14. .islower() method checks whether all cased characters in a string are lowercase and whether there is at least one cased character, returning a boolean (True or False).
print("bhqwdodh".islower())
print("1233@@".islower())
print("o123".islower())
# 15. isnumeric() method checks whether all characters in a string are numeric characters and returns a boolean (True or False).
print("1234".isnumeric())
print("²".isalnum())
print("hdhhd".isnumeric())
print("½".isnumeric()) # Output: True (Fractions are numeric)
print("²".isnumeric()) # Output: True (Superscripts are numeric)
print("Ⅷ".isnumeric()) # Output: True (Roman numerals are numeric)
# 16. isprintable() method checks whether all characters in a string are considered "printable" (meaning they occupy space on a screen or can be rendered visibly) and returns a boolean (True or False).
print("hello\rhi".isprintable())
print("hello world".isprintable())
# 17. isspace() method checks whether all characters in a string are whitespace characters (spaces, tabs, newlines, etc.) and returns a boolean (True or False).
print("\n".isspace())
print("jzjxsi".isspace())
print("\r".isspace())
print("\n\r\t".isspace())
# 18. istitle() - it is a method checks whether a string is formatted in title case (meaning every word starts with an uppercase letter, followed only by lowercase letters) and returns a boolean (True or False).
print("Hello hi ".istitle())
print("Welcome To Python".istitle())
'''text.title() takes your string and transforms it, returning a new string.
text.istitle() looks at your string and returns a boolean (True or False) telling you whether it's already properly capitalized.'''
# 19. isupper() - it is a method checks whether all cased characters in a string are uppercase and whether there is at least one cased character, returning a boolean (True or False).
print("JHWJGHREIOEJ".isupper())
print("F5583674@@@".isupper())
print("6748@&".isupper())
# 20. ljust() pads the right side of a string with spaces (or a specific character) to make sure the overall string reaches a specified total width.
print("hello ".ljust(7,"-"))
# 21. lower() method converts all uppercase characters in a string into lowercase and returns the new string.
print("d6HWDOWDk".lower())
# 22. lstrip() method removes leading characters (characters on the left side of a string) and returns the modified string.
print(" hello world ".lstrip())
print("@@@@@hello world@@@@@".lstrip("@"))