-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11 - Strings in Python.py
More file actions
93 lines (70 loc) · 1.91 KB
/
11 - Strings in Python.py
File metadata and controls
93 lines (70 loc) · 1.91 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
# Strings in Python Programming:-
"""
1) Strings are Immutable
2) String is ordered data structure and supports to indexing & Slicing
"""
s = "python"
print(type(s))
print(id(s))
print(s)
s = "java"
print(type(s))
print(id(s))
print(s)
d = "python"
print(type(d))
print(id(d))
print(d)
#indexing:- in this there are two categories:-1){Left Hand Side Indexing}:- In which indexing startted from left to right [ Where we start counting from indxing 0 ]
# 2){Right to Left Indexing} :- In which indexing started from right to left [ Where we count from -1 ]
s = "Python Sample String"
print(s[0])#p
print(s[-1])#g
print(s[1])#y
print(s[-2])#n
#Slicing:-
s = "Python Sample String"
print(s[0:5])#Pytho
print(s[0:6])#Python
print(s[7:])#Sample String (Started from 7 that is s of string and end is not mentioned so printed till last)
print(s[ :6])#Python (Since Start point is not mentioned so starting form zero by default)
#Stride:- is uses for printing of selected letters
# If the stride is possitive[+] it will give direction from left to right, negative[-] it will give direction from right to left
print(s[ : :2])#Pto apeSrn (I do not want initialize:do not want to print end:but print every second letter)
print(s[ : :3])#Ph metn (after 1st lettere[P] every third letter will be print)
print(s[ : :-1])#gnirtS elpmaS nohtyP ( negative indexing prints from right hand side, so in stride prints reversely )
print(s[ : :-2])#git lmSnhy ( after last first element it will print every second element from last )
# For printing ewvery letter of memtiones string
for value in s:
print(value)
"""P
y
t
h
o
n
S
a
m
p
l
e
S
t
r
i
n
g """
# in such a way for iterating characters:-
for value in s[ : :2]:
print(value)
"""P
t
o
a
p
e
S
r
n"""
help(str)