Python Strings Part 3

Python Strings – Part 3

Hello everyone. Welcome to our blog and a new chapter on our Python tutorial series, “Learn Python with me”. In this post today, we will continue discussing the topics of Python Strings. This is the third chapter in our tutorial series that will focus on Strings in Python. So, if you are visiting our blog for the first time, we would request you to go through the earlier chapters on Python Strings first to get the momentum of learning everything in order.


Our complete Python tutorial series: Learn Python


How to replace a string with another string?

In the Python programming language, you can use the replace() method to replace the substring with any other string in the main string. The format to do is shared here below.

stringname.replace(old, new)

The format shared above means replacing all the ‘old’ substring occurrences with a new substring in the main string. Let us look at an example now.

main_string='This is a wonderful thing'
print("The main string is ",main_string)
sub_string1='wonderful'
sub_string2='marvellous'
updated_main_string=main_string.replace(sub_string1,sub_string2)
print("The updated string is",updated_main_string)

Output

The main string is  This is a wonderful thing
The updated string is This is a marvellous thing

Using this replace method, you can replace substrings in any main string of a Python program.

How to split strings?

In the Python programming language, you can use the split() method to break down a string into pieces. For example, if you want to break a string whenever a comma (,) is found, you should follow the syntax below.

str.split(‘,’)

Symbols like the comma(,) are called separators because they separate or cut the strings.

string1="Newyork,London,Tokyo,Nairobi"
print("String1= ",string1)
string2=string1.split(',')
print("String2= ",string2)

Output

String1=  Newyork,London,Tokyo,Nairobi
String2=  ['Newyork', 'London', 'Tokyo', 'Nairobi']

This split() method will be helpful in cases when you want to display a set of numbers entered as input by a person, as shown in the example program below.

main_string=input("Enter a set of numbers separated by space")
#let us now separate the numbers using split() method
updated_string=main_string.split(' ')
#let us now display the numbers
for number in updated_string:
    print(number)

Output

Enter a set of numbers separated by space11 22 33 44 55
11
22
33
44
55

How to join strings?

In the Python programming language, you can use the join() method to combine any number of strings into a single string. Please find its syntax here below.

separator.join(str)

We know that the separators are any symbols or spaces separating each string. ‘str’ refers to a list or tuple of strings. Let us look at an example here below.

#Python program to join strings
string_tuple=('Sony','Apple','Samsung')
updated_string='-'.join(string_tuple)
print(updated_string)

Output

Sony-Apple-Samsung

How to change the case of a string in Python?

The python programming language offers you four methods title(), swapcase(), lower(), and upper() to play with the cases of strings in your Python code.

  • You can use the title() method to capitalize the first letter of each word in a string.
  • You can use the upper() method to convert all the characters of a string to uppercase.
  • You can use the lower() method to convert all the characters of a string to lowercase.
  • You can use the swapcase() method to convert the characters from lowercase to uppercase and vice versa.
#Change case in Python Strings
main_string='Sony Speakers is the best in market'
print('Upper case output: \n',main_string.upper())
print('Lower case output: \n',main_string.lower())
print('Swap case output: \n',main_string.swapcase())
print('Title output: \n',main_string.title())

Output

Upper case output: 
 SONY SPEAKERS IS THE BEST IN MARKET
Lower case output: 
 sony speakers is the best in market
Swap case output: 
 sONY sPEAKERS IS THE BEST IN MARKET
Title output: 
 Sony Speakers Is The Best In Market

How to check the beginning and end of the string?

You can use the startswith() and endswith() methods to check whether a string begins with a substring or ends with a substring. True is returned if it finds a match in the main string, or else False is returned.

#check beginning and ending of string
main_string='Sony Speakers is the best in market'
print(main_string.startswith('Sony'))
print(main_string.endswith('market'))

Output

True
True

String Testing Methods

Python programming language provides you with a set of methods to check the characteristics of a string in your Python code. These methods often return either True or False depending on the string you pass as input. You can use these methods on individual characters as well.

MethodDescription of the method
istitle()True is returned if the first character of each word in a string is a capital letter.
isupper()True is returned if all the characters are in upper case
islower()True is returned if all the characters are in lower case
isdigit()True is returned if the declared string contains only numerical values
isspace()True is returned if only the spaces are contained in a string
isalpha()True is returned if the declared string contains only alphabetical values
isalnum()True is returned if the declared string contains both alphabetical and numerical values.

Source

Scroll to Top