Skip to content

Commit 0907d58

Browse files
committed
added string examples of indexing,immutable and convert other datatype
1 parent 2ebe537 commit 0907d58

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

Diff for: Python-String/String-convert-in-other-data-type.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#Python String Demostration
2+
3+
#How to club with other character
4+
example = "test"
5+
print ('w' + example[1:])
6+
7+
#Converting String to list
8+
list_example=list(example)
9+
print ("String Data Type value is :",example)
10+
print ("List data type:",list_example)
11+
12+
#Converting String to set
13+
set_example=set(example)
14+
print ("String Data Type value is :",example)
15+
print ("Set data type:",set_example)
16+
17+
#Converting String to boolean
18+
boolean_example=bool(example)
19+
print ("String Data Type value is:",example)
20+
print ("Boolean data type:",boolean_example)
21+
22+
example =''
23+
#Converting String to boolean with blank value
24+
boolean_example=bool(example)
25+
print ("String Data Type value is:",example)
26+
print ("Boolean data type:",boolean_example)

Diff for: Python-String/String-immutable.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#Python String immutable Demostration
2+
3+
#Sting is immutable we can not change the value from indexing
4+
example="python"
5+
6+
print ("first character of string\n",example[0])
7+
8+
example[0] = "S"
9+
10+
print ("first character of string\n",example[0])
11+
12+
#TypeError: 'str' object does not support item assignment

Diff for: Python-String/String-indexingandSlicing.py

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#Demostration of Pythong String Indexing
2+
3+
example= "Python Scripting"
4+
5+
"""
6+
P Y T H O N S C R I P T I N G
7+
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 (Sequecebe )
8+
-16-15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 ()
9+
"""
10+
11+
print("Initial String: ")
12+
print(example)
13+
14+
# Printing First character
15+
print("\nFirst character of String is: ")
16+
print(example[0])
17+
#output would be P
18+
19+
print ("\n7th Character of String is:\n",example[7])
20+
#this will print only S
21+
22+
# Printing Last character
23+
print("\nLast character of String is: ")
24+
print(example[-1])
25+
#output would be G
26+
27+
# Printing 3rd to 12th character
28+
print("\nSlicing characters from 3-12: ")
29+
print(example[3:12])
30+
#output would be "hon Script"
31+
32+
# Printing characters between
33+
# 3rd and 2nd last character
34+
print("\nSlicing characters between " + "3rd and 2nd last character: ")
35+
print(example[3:-2])
36+
#hon Script
37+
38+
#printing character from 1 to end
39+
print ("\nCharacter from 1 to :")
40+
print (example[1:])
41+
42+
#printing character from 1 to 6
43+
print ("\nCharacter from 1 to 6")
44+
print (example[1:6])
45+
46+
#printing character from 1 to 12
47+
print ("\nCharacter from 1 to 12")
48+
print (example[1:12])
49+
50+
#printing character from 1 to 12 with increment by 2
51+
print ("\nCharacter from 1 to 12 with increment by 2")
52+
print (example[1:12:2])
53+
54+
55+
#printing character from 1 to 12 with increment by 2
56+
print ("\nCharacter from -3 to -1 with increment by 2")
57+
print (example[-9:-1:-1])
58+
59+
#printing character from -3 -15 in reverse order
60+
print ("\nCharacter from -3 -15 in reverse order")
61+
print (example[-3::-1])

0 commit comments

Comments
 (0)