Learn Python Part 4 String Methods in Python A complete introduction

Published: 10 May 2020
on channel: Bioinformatics and Computational Biology
291
8

This Video will cover the following topic:
String Methods
Changing the capitalizations of a string
Stripping the string
Reversing a string
Split a string
Replacing the occurrence of one substring with a new substring
Testing what a string is composed of
Format values into a string
Join a list of string
Test the starting and ending of string
String Module


CODE::
#String Methods
#Changing the capitalizations of a string
#str.upper
#str.lower
#str.capitalize
#str.title
#str.swapcase

s = 'This IS a String'
print('Upper case: ', s.upper())
print('Lower case: ', s.lower())
print('capitalize case: ', s.capitalize())
print('Title case: ', s.title())
print('Swape case: ', s.swapcase())

#Stripping the string
#str.strip
s = 'a line with leading and trailing space '
print("stripped string: ", s.strip())
s = "---a python prompt"
print("Stripped string: ", s.strip('-'))

Reversing a string
s = "hello"
print(reversed(s))
print("".join(reversed(s)))

Split a string
s = "this is a string"
print(s.split(' '))
s = 'this is a sentence'
print(s.split('e', maxsplit=1))
print(s.split('e', maxsplit=2))

Replacing the occurrence of one substring with a new substring
s = "Make sure to foo your sentence."
print(s.replace('foo', 'spam'))
s = "It can foo multiple examples of foo if you want."
print(s.replace('foo', 'spam'))
s = "It can foo multiple examples of foo if you want or you can limit the foo with the third argument"
print(s.replace('foo', 'spam', 1))

#Testing what a string is composed of
#str.isalpha
s = "Hello World"
s1 = "Hello2World"
s2 = "HelloWorld!"
s3 = "HelloWorld"
print("s: ", s.isalpha())
print("s1: ", s1.isalpha())
print("s2: ", s2.isalpha())
print("s3: ", s3.isalpha())

#str.isupper
s = "hello world"
s1 = "HELLO WORLD"
print("s: ", s.isupper())
print("s1: ", s1.isupper())

#str.islower
s = "hello world"
s1 = "HELLO WORLD"
print("s: ", s.islower())
print("s1: ", s1.islower())

#str.isdigit
#str.isnumeric
#str.isdecimal
#str.isspace

#Format values into a string
i = 10
f = 1.5
s = "foo"
l = ['a', 1, 2]
d = {'a': 1, 2: 'foo'}
print("{}{}{}{}{}".format(i,f,s,l,d))
print(str.format("{}{}{}{}{}", i , f , s, l, d))
s = "I am from Australia. I love cupcakes from Australia!"
print("I am from {}. I love cupcakes from {}!".format("Australia", "Australia"))
print("I am from {0}. I love cupcakes from {0}!".format("Australia"))

#Join a list of string
l = ["once", "upon", "a", "time"]
print("----".join(l))

#Counting number of times a substring into a string
#str.count(sub[,start[,end)
s = "She sells seashells by the seashore."
print(s.count("sells"))

Test the starting and ending of string
#str.startswith
#str.endswith
s = "this is a test string"
print(s.startswith("t"))
print(s.startswith("T"))
print(s.endswith("ing"))

String Module
import string
print(string.ascii_letters)
print(string.ascii_lowercase)
print(string.ascii_uppercase)
print(string.digits)
#string.digits
#string.hexdigits
#string.octaldigits
#string.punctuations
print(string.printable)


On this page of the site you can watch the video online Learn Python Part 4 String Methods in Python A complete introduction with a duration of hours minute second in good quality, which was uploaded by the user Bioinformatics and Computational Biology 10 May 2020, share the link with friends and acquaintances, this video has already been watched 291 times on youtube and it was liked by 8 viewers. Enjoy your viewing!