Repository navigation
Expand file tree
/
Copy path094-String-Inbuilt-Methods.py
More file actions
120 lines (100 loc) · 4.14 KB
/
Copy path094-String-Inbuilt-Methods.py
File metadata and controls
120 lines (100 loc) · 4.14 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
"""
===============================================================================
File Name : 94-String-Inbuilt-Methods.py
Description : String Inbuilt Methods in Python
Author : Shaik Mahaboob Basha
===============================================================================
Definition:
Python provides many built-in methods to perform different operations on
strings such as changing case, searching, replacing, counting, splitting,
joining, and checking string properties.
Some Common String Methods:
1. upper()
2. lower()
3. title()
4. capitalize()
5. swapcase()
6. replace()
7. find()
8. count()
9. startswith()
10. endswith()
Syntax:
string_name.method_name()
Example:
text.upper()
"""
# -----------------------------------------------------------------------------
# Creating a string.
# -----------------------------------------------------------------------------
text = "welcome to python programming"
# -----------------------------------------------------------------------------
# Displaying the original string.
# -----------------------------------------------------------------------------
print("Original String :", text)
# Output:
# Original String : welcome to python programming
print()
# -----------------------------------------------------------------------------
# Converting the string into uppercase.
# -----------------------------------------------------------------------------
print("upper() :", text.upper())
# Output:
# upper() : WELCOME TO PYTHON PROGRAMMING
# -----------------------------------------------------------------------------
# Converting the string into lowercase.
# -----------------------------------------------------------------------------
print("lower() :", text.lower())
# Output:
# lower() : welcome to python programming
# -----------------------------------------------------------------------------
# Converting the first letter of each word into uppercase.
# -----------------------------------------------------------------------------
print("title() :", text.title())
# Output:
# title() : Welcome To Python Programming
# -----------------------------------------------------------------------------
# Converting the first letter of the string into uppercase.
# -----------------------------------------------------------------------------
print("capitalize() :", text.capitalize())
# Output:
# capitalize() : Welcome to python programming
# -----------------------------------------------------------------------------
# Swapping uppercase letters to lowercase and vice versa.
# -----------------------------------------------------------------------------
print("swapcase() :", text.swapcase())
# Output:
# swapcase() : WELCOME TO PYTHON PROGRAMMING
print()
# -----------------------------------------------------------------------------
# Replacing a word in the string.
# -----------------------------------------------------------------------------
print("replace() :", text.replace("python", "Java"))
# Output:
# replace() : welcome to Java programming
print()
# -----------------------------------------------------------------------------
# Finding the position of a word.
# -----------------------------------------------------------------------------
print("find('python') :", text.find("python"))
# Output:
# find('python') : 11
# -----------------------------------------------------------------------------
# Counting the occurrence of a character.
# -----------------------------------------------------------------------------
print("count('o') :", text.count("o"))
# Output:
# count('o') : 5
print()
# -----------------------------------------------------------------------------
# Checking whether the string starts with a word.
# -----------------------------------------------------------------------------
print("startswith('welcome') :", text.startswith("welcome"))
# Output:
# startswith('welcome') : True
# -----------------------------------------------------------------------------
# Checking whether the string ends with a word.
# -----------------------------------------------------------------------------
print("endswith('programming') :", text.endswith("programming"))
# Output:
# endswith('programming') : True