String Operations

Python slicing is about obtaining a sub-string from the given string

syntax: the start index and the end index
# string[start_index:ending_index]
x = "Hello, World!"
print(x[2:5])
# output: llo

# removing start point index string[:ending_index]
x = "Hello, World!"
print(x[:5])
# output: Hello

+ and * Operators

# operator + and *
print('using operator + for numeric type data')
print(10 + 10)
print('using operator + for String type data')
print('10' + '10')
print('using operator * for numeric type data')
print(10 * 10)
print('using operator * for String type data')
print('10' * 10)

len(), lower(), upper(), capitalize(), title()…methods

# len()
my_string = 'python is the best programming language'
print("String Value\n", my_string)
print("Length of created string is: ", len(my_string)) #len() function

# lower() - converts into ALL Lower case
MY_ALL_CAPS_STRING = "PYTHON IS THE BEST PROGRAMMING LANGUAGE"
print(MY_ALL_CAPS_STRING.lower())

# upper() - Converts in All CAPITAL case
print(my_string.upper())

# capitalize() - Converts in capitalize case
print(my_string.capitalize())

# title() - Converts in Title case
print(my_string.title())

replace(), join(), reverse()…method

# using replace() method to change some text in string
my_string = 'I am learning python' # old string
new_string = my_string.replace('learning','writing') 
# fetching string and changing learning to writing
#storing it into new_string
print(f'Old String:\t{my_string}\nNew String:\t{new_string}') # printing the both strings

#example of join()
print(‘Let’s use join on the new string’)
c= ",".join(new_string)
print(c)

# reversing a string
some_palindrome = 'Was it a car or a cat I saw'
# removing all spaces from string
new_reduced_string = (some_palindrome.replace(" ",""))
print(f'simple:\t{some_palindrome} :: Reversed:\t',''.join(reversed(some_palindrome)))
print(f'reduced simple:\t{new_reduced_string} :: Reversed:\t',''.join(reversed(new_reduced_string)))

The split() function

x = 'Python'
new_string = x.split('y')
print(new_string)
d= "Python is my favorite"
string_edit= d.split("o")
print(string_edit)

# Dissecting URL using string manipulation - split() function
url_string = 'https://www.example.org/airport/apparel'
list_of_url = url_string.split('/')

# returns each element in form of a list
print(list_of_url) 
print(type(list_of_url)) 

# output; ['https:', '', 'www.example.org', 'airport', 'apparel']
	<class 'list'>