Skip to content

Commit e28ccd1

Browse files
author
ashegde
committed
feat(example): strings
1 parent cea3982 commit e28ccd1

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

codes/session_1/string.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Open terminal > python3
2+
# Start typing below commands and see the output
3+
4+
# Use single quote
5+
'Ashwin'
6+
7+
'Ashwin\'s'
8+
9+
# Use double quote
10+
"Ashwin's"
11+
12+
"Hello \"Ashwin\" "
13+
14+
name = 'Ashwin Hegde\nashwin.hegde3@gmail.com'
15+
name # Without print() => \n has no effect
16+
print(name) ## \n means new line
17+
18+
print('C:\python\name')
19+
print(r'C:\python\name') # r before single quote
20+
21+
# String in multi line, 1 way using triple-quotes """...""" or '''...'''
22+
print("""\
23+
Usage: python-cli [options]
24+
-h help
25+
-H hostname
26+
""")
27+
28+
# String can be concate using +
29+
'Ashwin' + 'Hegde'
30+
31+
# 2 or more string only (no variable or no expression) kept next to each other are automatically concate
32+
'Ash' 'win' ' ' 'Hegde'
33+
34+
# String can be repeat using *
35+
'Ashwin will' + 3 * ' Win'
36+
37+
# Handling long strings
38+
text = ('Hello World! My name is Ashwin '
39+
'and we are learning Python Syntax!')
40+
text
41+
42+
# String index (starts from left)
43+
# and negative index (starts from right and from -1)
44+
name = 'Ashwin'
45+
name[0] # A
46+
name[5] # n
47+
name[7] # IndexError: String index out of range
48+
name[-1] # n
49+
name[-3] # w
50+
51+
# Slicing
52+
name[3:6] # win => 3 included, 6 excluded
53+
name[3:] # win
54+
name[:3] # Ash
55+
56+
# How Slice works?
57+
# +---+---+---+---+---+---+
58+
# | P | y | t | h | o | n |
59+
# +---+---+---+---+---+---+
60+
# 0 1 2 3 4 5 6
61+
# -6 -5 -4 -3 -2 -1
62+
63+
# String length
64+
len('Ashwin') # 6
65+

0 commit comments

Comments
 (0)