0% found this document useful (0 votes)
29 views

Code Strings

Uploaded by

9316213351
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Code Strings

Uploaded by

9316213351
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Code for section "Strings"

of the course "Python in Depth" by Deepali Srivastava

Introduction to Strings

s1 = 'flawsome'
s2 = "Keep going, you are getting there'"
s1
s2
len(s1)
s1[3]
s2[len(s2)-1]
s2[-1]
s2[-2]
s2[50]
s2[-30]

String Slicing
>>>s = 'homogeneous'
>>>s[2:6]
>>>s1 = s[4:7]
>>>s1
>>>s
>>>id(s)
>>>s = s[3:7]
>>>s
>>>id(s)
>>>s = "homogeneous"
>>>s[:4]
>>>s[5:]
>>>s[:]
>>>scopy = s[:]
>>>scopy
>>>s[:-1]
>>>s[:-3]
>>>s[3:3]
>>>s[2:100]
>>>s[-30:6]
>>>s[100]
>>>s[-30]
>>>s = 'Today is the day.'
>>>s[3:13:2]
>>>s[::2]
>>>s[::3]
>>>s[::-1]

"Python in Depth" by Deepali Srivastava 1


Combining and repeating strings
>>>'ab' + 'cd'
>>>name = 'dev'
>>>'hello' + name
>>>'hello' + ' ' + name
>>>'hello ' + ' ' + name
>>>s = 'hello' + ' ' + name
>>>s
>>>name * 3
>>>3 * name
>>>'abc' * 5
>>>name+='ank'
>>>name
>>>name *= 3
>>>name
>>>s1 = 'Good Morning !'
>>>s2 = 'Bye Bye See you'
>>>s3 = s1[:4] + s2[:3]
>>>s4 = s1[:4]*3 + s1[4:-1] + s1[-1]*5
>>>'abc' 'def' 'hij'
>>>'ood' in s1
>>>'p' in s1
>>>"you" not in s2

String Methods-1

>>>s = "Life's a journey … not a race"


>>>s.upper()
>>>s
>>>s.capitalize()
>>>s
>>>s.isupper()
>>>s.isnumeric()

>>>s2 = 'Be a voice, not an echo'


>>>s2.ljust(40)
>>>s2.rjust(40)
>>>s2.center(40)
>>>s2.center(4)

>>>s1 = ' All is well !!!'


>>>s1.lstrip()
>>>s1.rstrip('!')
>>>s1.rstrip('!').upper()
>>>s1.rstrip('!').upper().strip()

"Python in Depth" by Deepali Srivastava 2


String Methods -2
>>>s = '''Count your blessings instead of your crosses;
Count your gains instead of your losses.
Count your joys instead of your woes;
Count your friends instead of your foes. '''

>>>s1 = """Pop, pop, popcorn, Popping in the pot!


Pop, pop, popcorn, Eat it while it's hot!
Pop, pop, popcorn, Butter on the top!
When I eat popcorn, I can't stop!"""

>>>s
>>>print(s)
>>>s.find('Count')
>>>s.rfind('Count')
>>>s.find("count")
>>>s.index("count")
>>>s1.count('pop')
>>>s1.startswith("Pop")
>>>s1.endswith(".")
>>>s1.endswith("!")
>>>s.find('Count',15,100)
>>>s = 'Dev; 22; male; graduate; Jaipur'
>>>s.index(';')
>>>s[s.index(';') : ]
>>>s[s.index(';') +1: ]
>>>s2 = s[s.index(';')+1 : ]
>>>s2
>>>s2 = s[s.rindex(';')+1 : ]
>>>s2
>>>s2 = [s.index(';')+1 : s.rindex(';')]
>>>s2 = s[ : s.index('xy')]
>>>s2 = s[:s.find('xy')]
>>>s2
>>>s2 = s.replace('Count', 'Enumerate')
>>>print(s2)
>>>s2 = s.replace('Count', 'Enumerate', 3)
>>>print(s2)
>>>s2 = s.replace('your','')
>>>print(s2)
>>>s2 = s.replace('your ','')
>>>s2 = s.replace('your','').replace(' ', ' ')

"Python in Depth" by Deepali Srivastava 3


Escape Sequences and Raw Strings

print("How\nare\nyou")
len("How\nare\nyou")
print("How\tare\nyou")
print("H\el\lo")
print('C:\textfiles\newFile')
print('C:\\textfiles\\newFile')
print('Don't run')
print('Don\'t run')
print(r'C:\Deepali\newFiles')
print('C:\Deepali\newFiles')

String Formatting
>>>name = 'Dev'
>>>age = 23
>>>wt =43.567

>>>s = 'My name is ' + name + ', I am ' + str(age) + ' years old and
my weight is ' + str(wt) + ' kg'
>>>s
>>>print('My name is', name, ', I am ', age, ' years old and my
weight is ', wt, ' kg')

>>>s = f'My name is {name}, I am {age} years old and my weight is


{wt} kg'
>>>s
>>>s = f'After 10 years { name.upper() } will be {age+10} years old'
>>>s

>>>s = f'He is {{{name},{age} }} '


>>>s

>>>s = f'His name is {name:8} and he is {age:6} years old'


>>>s

>>>f'His name is {name:>8} and he is {age:<6} years old'


>>>s
>>>f'His name is {name:^8} and he is {age:^6} years old'

>>>s = f'Age is {age:f} and weight is {wt}'


>>>s
>>>s = f'Age is {age:.3f} and weight is {wt}'
>>>s = f'Age is {age:10.3f} and weight is {wt}'
>>>s = f'Age is {age:<10.3f} and weight is {wt}'
>>>s = f'Age is {age:<10.3f} and weight is {wt:.3}'

"Python in Depth" by Deepali Srivastava 4


>>>s
>>>s = f'Age is {age:<10.3f} and weight is {wt:8.3}'
>>>s
>>>s = f'Age is {age:<10.3f} and weight is {wt:8.3f}'
>>>s = f'Age is {age:<10.3f} and weight is {wt:8.2f}'
>>>name = 'Dev'
>>>age = 23
>>>print(f'My name is {name:*^10} and age is {age:->12}')

>>>print( fr'\name: {name}' )

>>>s = f'''My name is {name}, I am {age} years old


and my weight is {wt} kg'''

>>>print(s)

>>>num = 1247

>>>print(f"{num:x}")
>>>print(f"{num:o}")
>>>print(f"{num:b}")

>>>num1 = 0.00000082478
>>>num2 = 3345600000000

>>>print(f'{num1:e}')
>>>print(f'{num2:e}')
>>>print(f'{num2:E}')

>>>s = f'{num2:,}'

"Python in Depth" by Deepali Srivastava 5

You might also like