Saturday, July 25, 2026
Home General Working with String in Python

Working with String in Python

Create a String

Strings can simply be defined by use of single ( ‘ ), double ( ” ) or triple ( ”’ ) inverted commas. Strings enclosed in triple quotes ( ”’ ) can span over multiple lines. A few things to keep in mind about strings:

  • Strings are immutable in Python, so you can not change the content of a string.
  • Function len() can be used to get length of a string
  • You can access the elements using indexes as you do for lists
  • You can use ‘+’ operator to concatenate two strings

String ="String elements can also be accessed using index numbers, just like lists"

print (String[0:7])

#Above print command displays "String " on screen.

TASKS TO DO

# Create a string str1
str1 = “Introduction with strings”

# Now store the length of string str1 in variable str_len
str_len = len(str1)

str_new = “Machine Learning is awesome!”
# Print last eight characters of string str_new (the length of str_new is 28 characters).
print(str_new[20:28])

str2 = “I am doing a course Introduction to Hackathon using ”
str3 = “Python”

# Write a line of code to store concatenated string of str2 and str3 into variable str4
str4 = str2 + str3

Try it yourself in DataCamp here

datasagarhttp://www.DataSagar.com
The author of this blog post is a technology fellow, an IT entrepreneur, and Educator in Kathmandu Nepal. With his keen interest in Data Science and Business Intelligence, he writes on random topics occasionally in the DataSagar blog.
RELATED ARTICLES

How to setup DNS Records in Cloudflare – Guide for Website Owners

Whether you're launching your first website, connecting a custom email address, or moving your domain to a new hosting provider, you've probably...

DeepSeek AI: The Open-Source AI Disruptor Shaking Up the Tech World

DeepSeek AI is revolutionizing the artificial intelligence landscape with its open-source, cost-effective, and highly customizable model. Built on cutting-edge transformer architecture and fine-tuned using reinforcement learning with human feedback (RLHF), DeepSeek delivers human-like text generation and accurate responses. Its efficient training techniques reduce computational costs by up to 40%, while its foundation on open-source frameworks like PyTorch and TensorFlow ensures seamless integration for developers. Whether you're a startup, a developer, or a large enterprise, DeepSeek offers unparalleled transparency, affordability, and performance, making it a game-changer in the world of AI. Discover why DeepSeek is outshining competitors like ChatGPT and Google Gemini in this comprehensive guide.

Useful Python Libraries for Data Science & ML Enthusiasts

Hi there, DataSagar here! If you're as passionate about data science as I am, then you know how overwhelming it can be...

Most Popular

How to setup DNS Records in Cloudflare – Guide for Website Owners

Whether you're launching your first website, connecting a custom email address, or moving your domain to a new hosting provider, you've probably...

DeepSeek AI: The Open-Source AI Disruptor Shaking Up the Tech World

DeepSeek AI is revolutionizing the artificial intelligence landscape with its open-source, cost-effective, and highly customizable model. Built on cutting-edge transformer architecture and fine-tuned using reinforcement learning with human feedback (RLHF), DeepSeek delivers human-like text generation and accurate responses. Its efficient training techniques reduce computational costs by up to 40%, while its foundation on open-source frameworks like PyTorch and TensorFlow ensures seamless integration for developers. Whether you're a startup, a developer, or a large enterprise, DeepSeek offers unparalleled transparency, affordability, and performance, making it a game-changer in the world of AI. Discover why DeepSeek is outshining competitors like ChatGPT and Google Gemini in this comprehensive guide.

Useful Python Libraries for Data Science & ML Enthusiasts

Hi there, DataSagar here! If you're as passionate about data science as I am, then you know how overwhelming it can be...

Nepal Government allowing IT Companies to Invest Abroad

In a landmark decision that signals Nepal’s intent to embrace globalization and technological advancement, the government has now allowed Nepali IT companies...

Recent Comments