Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Python string length
Next article icon

Python provides an wide range of built-in methods that make string manipulation simple and efficient. In this article, we'll explore several techniques for modifying strings inPython.

Start with doing a simplestringmodification by changing the its case:

Changing Case

One of the simplest ways to modify a string is by changing its case usingstring.upper() method which converts all characters in the string to uppercase.

Python
s="hello world"print(s.upper())

Output
HELLO WORLD

Other methods of changing case in a python strings are:

  • lower(): Converts all characters in the string to lowercase.
  • capitalize(): Capitalizes the first character of the string and makes the rest lowercase.
  • title():Capitalizes the first letter of each word in the string.
  • swapcase(): Swap the cases of all characters in a string
  • capitalize(): Convert the first character of a string to uppercase

Let's take a look at other cases of string modification:

Replacing Substrings

We can replace parts of a string usingreplace() method. This method takes two arguments: 1) old substring we want to replace and 2) new substring we want to replace it with.

Example:

Python
s1="I love Python"s2=s1.replace("Python","programming")print(s2)

Output
I love programming

The replace() method returns a new string, so the original string is not modified in place (since strings are immutable in Python).

Trimming Whitespace

Sometimes, strings may have unwanted spaces at the beginning or end. To remove these, we can use thestrip() method. This method removes any leading and trailing whitespace from the string.

Python
s="   Hello World!   "print(s.strip())

Output
Hello World!

We can also uselstrip() to remove leading spaces andrstrip() to remove trailing spaces.

Concatenating Strings

Concatenating strings means joining two or more strings together. In Python, we can do this using the + operator.

Python
s1="Hello"s2="World"print(s1+" "+s2)

Output
Hello WorldPython is awesome

We can also use join() when concatenating alist of strings.

Slicing Strings

String slicing allows you to extract a portion of a string. We can specify a starting and ending index and Python will return a substring. We can also use the step parameter to skip characters.

Python
s="Hello, Python!"print(s[0:5])# Output: "Hello" (characters from index 0 to 4)print(s[7:])# Output: "Python!" (characters from index 7 to the end)print(s[:5])# Output: "Hello" (characters from the start to index 4)print(s[::2])# Output: "Hoo yhn" (every second character)

Output
HelloPython!HelloHlo yhn

Checking for Substrings

To check if a string contains a certain substring, we can usein keyword.

Python
s="Python is fun"print("Python"ins)

Output
True

The in keyword returns True if the substring is found . We can also usefind() method which returns the index of the first occurrence of the substring (or -1 if it's not found).

Formatting Strings

Python allows we to format strings in a readable way usingf-strings (formatted string literals), which were introduced in Python 3.6. We can insert variables or expressions inside a string using curly braces {}.

Python
s="Geek"print(f"name -{s}")

Output
name - Geek

For earlier versions of Python, we can use theformat() method.

You can learn more about string methods with ourString Methods Article.


Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp