Skip to content

Commit

Permalink
Python string slice examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Pankaj Kumar committed Oct 8, 2018
1 parent cee7497 commit 7423bb4
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions Python-3/basic_examples/strings/string_slice_substring.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
s = 'HelloWorld'

# s[:i] + s[i:] == s

print(s[:])

print(s[::])

s = 'HelloWorld'
first_five_chars = s[:5]
print(first_five_chars)

third_to_fifth_chars = s[2:5]
print(third_to_fifth_chars)

# reverse a string using slicing
s = 'HelloWorld'
reverse_str = s[::-1]
print(reverse_str)

s1 = s[2:8:2]
print(s1)

s1 = s[8:1:-1]
print(s1)

s1 = s[8:1:-2]
print(s1)

s1 = s[-4:-2]
print(s1)

s1 = s[100:]
print(s1)

0 comments on commit 7423bb4

Please sign in to comment.