Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is string manipulation Python?
3.
Concatenation of Strings
4.
Replace Part of a String
5.
Count characters
6.
Split a String
7.
find( ) function
8.
join() Function
9.
Remove a Prefix or a Suffix
10.
Some More Important String Functions
11.
 
12.
Frequently Asked Questions
12.1.
What are strings in Python?
12.2.
What is string manipulation?
12.3.
Why is string manipulation important?
12.4.
What are the two built-in string manipulation function in Python?
13.
Conclusion
Last Updated: Mar 27, 2024
Easy

String Manipulation in Python

Author GAZAL ARORA
1 upvote
gp-icon
Basics of Python
Free guided path
7 chapters
99+ problems
gp-badge
Earn badges and level up

Introduction

A string is known as a sequence of characters, and string manipulation is something we programmers do all the time. In Python, Strings are arrays of bytes that represent Unicode characters.

Python does not contain any character variables compared to other programming languages. In Python, characters are instead strings of unit length.

String manipulation is the process of manipulating and analyzing strings. It involves modification and parsing of strings to use and change their data.

String manipulation in python

Python has several built-in functions in the standard library to help programmers manipulate strings in different ways. Let's learn more about these functions.

Also See, Floor Division in Python

What is string manipulation Python?

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Concatenation of Strings

Concatenation means to join two or more strings into a single string. + operator is used to concatenate strings. For example,

# concatenating words Coding and Ninjas.
word1 = 'Coding'
word2 = ' Ninjas'
print(word1 + word2)

Output:

Also see, Merge Sort Python
 

Replace Part of a String

The replace() method is used to replace a part of the string with another string. As an example, let's replace 'ing' in Coding with 'ers,' making it Coders.

string = 'Coding'
string = string.replace('ing', 'ers')
print(string)

Output:

Also See, Divmod in Python and Python Operator Precedence

Count characters

count() method is used to count a particular character in the string.

string = 'Coding Ninjas'

# count the character 'i'.
count1 = string.count('i')
print("Count of 'i': ", count1)

# Count spaces.
count2 = string.count(' ')
print("Number of spaces:", count2)

Output:

Split a String

It is a very common practice to split a string into smaller strings. In Python, we use the split() function to do this. It splits the string from the identified separator and returns all the pieces in a list.

Syntax:

string.split(separator, maxsplit)

 

  • separator: the string splits at this separator.
  • maxsplit (optional): tells how many splits to do.

For example,

string = 'Welcome to Coding Ninjas'
# Splitting across the whitespace.
myList = string.split(" ")
print(myList)

Output:

Note: The split() method, by default, takes any consecutive number of whitespaces as delimiters.

Learn more about: Swapcase in Python

find( ) function

The find() function looks for the first occurrence of the provided value. If the value is not found in that string, it returns -1. The value can be any character or a substring.

string = 'Welcome to Coding Ninjas'
index = string.find('t')

print(index)

Output:

Check out Escape Sequence in Python

join() Function

This method is used to join a sequence of strings with some operator. The join() operator will create a new string by joining every character of the sequence with a specified character, including whitespaces.

Note: The join() method only accepts strings. If any element in the iteration is of a different type, an error will be thrown.

Syntax:

"character".join(seq)

 

  • character: The character from which the strings will be joined.
  • seq: Sequence of the strings to be joined.
string = 'Welcome to Coding Ninjas'
# joining the above-given characters of the string with ‘,’.
string = ",".join(string)

print(string)

Output:

You can try it on online python compiler.

Remove a Prefix or a Suffix

You can use the removeprefix() method to remove a prefix from a string.

Syntax:

string.removeprefix('prefix')

You can use the removesuffix() method to remove a suffix from a string.

Syntax:

string.removesuffix('suffix')

Note: The removeprefix() and removesuffix() were added in Python 3.9. So, these will not work for versions below 3.9.

Must Read Python List Operations

Some More Important String Functions

Syntax

Function

string.upper()

 

To transform all the characters of the string into uppercase.

 

string.lower()

 

To transform all the characters of the string into lowercase.

string.title() 

To transform the first letter of a word into the upper case and the rest of the characters into the lower case.

string.swapcase() 

To transform the upper case characters into lower case and vice versa.

string.capitalize() 

To transform the first character in the string to the upper case.

string.isupper() 

Returns true if all the alphabetic characters of the string are upper case.

string.islower() 

Returns true if all the alphabetic characters of the string are lower case.

string.Endswith() 

Return true if the string ends with a specific value.

string.Startswith() 

Return true if the string starts with a specific value.

string.index(‘character’) 

Return the position of the character.

 

 

You can also read about the Multilevel Inheritance in Python.

Frequently Asked Questions

What are strings in Python?

A string is a sequence of alphabets, words, or other characters. It is a simple data structure that serves as the foundation for data manipulation. Strings in Python are "immutable." They can't be modified once they're created.

What is string manipulation?

String manipulation is the process of manipulating and analyzing strings. It includes a variety of processes involving the modification and parsing of strings to use and change their data.

Why is string manipulation important?

Because most applications deal with text and interact with the user, string manipulation is important for new programmers to learn. A string is a variable that contains a string of one or more alphanumeric characters. A string can usually be manipulated to provide information or change its contents.

What are the two built-in string manipulation function in Python?

Two common built-in string manipulation functions in Python are:

  1. str.upper(): Converts a string to uppercase.
  2. str.lower(): Converts a string to lowercase. These functions are used for changing the case of characters within a string.

Conclusion

In conclusion, string manipulation in Python involves various operations to modify and process strings, including altering case, concatenating, slicing, searching, and formatting. These operations are essential for text processing, data manipulation, and generating dynamic content in Python programs.

So folks, now you are ready to play with the strings. Start today and use the above methods to master strings.

Check out this article - C++ String Concatenation

Recommended Readings:

Want to learn Python but don't know where to start?

Start here. Coding Ninjas provide this amazing course on Python. If you are just getting into coding and want to build a strong foundation, this course is for you.

Recommended problems -

You can also use Coding Ninjas Studio to practice various DSA questions asked in many interviews. It will assist you in mastering efficient coding techniques, and you will also get interview experiences from people working in big companies.

Previous article
Mutable and Immutable Objects in Python
Next article
String Reverse in Python
Guided path
Free
gridgp-icon
Basics of Python
7 chapters
127+ Problems
gp-badge
Earn badges and level up
Live masterclass