-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path096-Split-Method.py
More file actions
89 lines (72 loc) · 3.13 KB
/
Copy path096-Split-Method.py
File metadata and controls
89 lines (72 loc) · 3.13 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
"""
===============================================================================
File Name : 96-Split-Method.py
Description : Split Method in Python
Author : Shaik Mahaboob Basha
===============================================================================
Definition:
The split() method is used to split a string into a list of substrings based
on a specified separator. If no separator is provided, the string is split
using whitespace.
Syntax:
string_name.split(separator)
Example:
text.split()
text.split(",")
"""
# -----------------------------------------------------------------------------
# Creating a string with spaces.
# -----------------------------------------------------------------------------
text = "Python is easy to learn"
# -----------------------------------------------------------------------------
# Displaying the original string.
# -----------------------------------------------------------------------------
print("Original String :", text)
# Output:
# Original String : Python is easy to learn
print()
# -----------------------------------------------------------------------------
# Splitting the string using whitespace.
# -----------------------------------------------------------------------------
words = text.split()
# -----------------------------------------------------------------------------
# Displaying the list of words.
# -----------------------------------------------------------------------------
print("After split() :", words)
# Output:
# After split() : ['Python', 'is', 'easy', 'to', 'learn']
print()
# -----------------------------------------------------------------------------
# Creating a comma-separated string.
# -----------------------------------------------------------------------------
fruits = "Apple,Banana,Mango,Orange"
# -----------------------------------------------------------------------------
# Displaying the comma-separated string.
# -----------------------------------------------------------------------------
print("Fruit String :", fruits)
# Output:
# Fruit String : Apple,Banana,Mango,Orange
print()
# -----------------------------------------------------------------------------
# Splitting the string using a comma as the separator.
# -----------------------------------------------------------------------------
fruit_list = fruits.split(",")
# -----------------------------------------------------------------------------
# Displaying the list of fruits.
# -----------------------------------------------------------------------------
print("After split(',') :", fruit_list)
# Output:
# After split(',') : ['Apple', 'Banana', 'Mango', 'Orange']
print()
# -----------------------------------------------------------------------------
# Displaying the total number of words.
# -----------------------------------------------------------------------------
print("Total Words :", len(words))
# Output:
# Total Words : 5
# -----------------------------------------------------------------------------
# Displaying the total number of fruits.
# -----------------------------------------------------------------------------
print("Total Fruits :", len(fruit_list))
# Output:
# Total Fruits : 4