Comptt

Comptt
  • Home
  • Features
  • _Multi DropDown
  • __Dropdown 1
  • __Dropdown 2
  • __Dropdown 3
  • _ShortCodes
  • _Sitemap
  • _Error Page
  • Documentation
  • _Web Doc
  • _Video Doc
  • Download This Template
January 29, 2023 python

 Strings in Python


What are the strings ?

In Python, strings are sequences of characters that can be used to store and manipulate text.
They are commonly used to represent words, sentences and other pieces of text in a program.
Python strings is a collection of Unicode characters.

String properties

Python strings can be enclosed in single, double or triple quotes.

'Hello World'
"Hello World"
' ' 'Hello World' ' '
" " "Hello World" " "
 print('Hello World')
print("Hello World")
print('''Hello World''')
print("""Hello World""")

//Output
Hello World
Hello World
Hello World
Hello World


Python strings are immutable 

An object whose internal state can be changed is mutable. On the other hand, immutable doesn't allow any change in the object once it has been created.
So the strings are immutable in Python they doesn't allow any change in the object.

Example:

 x = "Hello World"
x = "Hello"
x[0] = M
print(x)

//Output
TypeError: 'str' object does not support item assignment


String concatenation 

String can be concatenated using + operator
 first_name = "Elon"
last_name = "-Musk"
full_name = first_name + last_name
print(full_name)

//Output
Elon-Musk

String slicing

In Python, string slicing is a way to extract a portion of a string by specifying the start and end index of the characters you want to extract.

The syntax for string slicing in Python is  string[start: end: step]

Where "string" is the name of the string variable, "start" is the index of the first character to include in the slice, and "end" is the index of the first character to exclude from the slice.

                                                                                   



In Python, strings can be indexed using either positive or negative numbers.

A positive index refers to the position of an element in a string, starting from 0 for the first character.
From the first table the string "ASTRING" has a 'A'  at index 0, 'S' at index 1, and so on. 

A negative index refers to the position of an element in a string, starting from -1 for the last character.
From the second table the string "ASTRING" has a 'G' at index -1, 'N' at index -2, and so on.

String slicing is a way to extract a portion of a string by specifying a start and end index.

Here, are some examples of string slicing in Python

1. Extracting a single character:
 string = "hello"
char = string[1]
print(char)

//Output
"e"
2.  Extracting a substring: 
 string = "hello world"
substring = string[7:12]
print(substring)

//Output
"world"
  1. Extracting a substring from the end of the string:
 string = "hello world"
substring = string[-6:]
print(substring)

//Output
"world"
  1. Extracting a substring from the start of the string:
 string = "hello world"
substring = string[:5]
print(substring)

//Output
"hello
  1. Extracting every nth character:
 string = "hello world"
substring = string[::2]
print(substring)

//Output
"hlowrd"
  1. Extracting a substring in reverse order:
 string = "hello world"
substring = string[::-1]
print(substring)

//Output
"dlrow olleh" 
  1. Extracting a substring using both positive and negative indexing:
 string = "hello world"
substring = string[-6:5]
print(substring)

//Output
"wor"

Strings Methods

In Python, strings have several built-in methods that can be used to perform various operations on them.

Method

Description

capitalize()

Converts the first character to upper case

casefold()

Converts string into lower case

center()

Returns a centered string

count()

Returns the number of times a specified value occurs in a string

encode()

Returns an encoded version of the string

endswith()

Returns true if the string ends with the specified value

expandtabs()

Sets the tab size of the string

find()

Searches the string for a specified value and returns the position of where it was found

format()

Formats specified values in a string

format_map()

Formats specified values in a string

index()

Searches the string for a specified value and returns the position of where it was found

isalnum()

Returns True if all characters in the string are alphanumeric

isalpha()

Returns True if all characters in the string are in the alphabet

isascii()

Returns True if all characters in the string are ascii characters

isdecimal()

Returns True if all characters in the string are decimals

isdigit()

Returns True if all characters in the string are digits

isidentifier()

Returns True if the string is an identifier

islower()

Returns True if all characters in the string are lower case

isnumeric()

Returns True if all characters in the string are numeric

isprintable()

Returns True if all characters in the string are printable

isspace()

Returns True if all characters in the string are whitespaces

istitle()

Returns True if the string follows the rules of a title

isupper()

Returns True if all characters in the string are upper case

join()

Converts the elements of an iterable into a string

ljust()

Returns a left justified version of the string

lower()

Converts a string into lower case

lstrip()

Returns a left trim version of the string

maketrans()

Returns a translation table to be used in translations

partition()

Returns a tuple where the string is parted into three parts

replace()

Returns a string where a specified value is replaced with a specified value

rfind()

Searches the string for a specified value and returns the last position of where it was found

rindex()

Searches the string for a specified value and returns the last position of where it was found

rjust()

Returns a right justified version of the string

rpartition()

Returns a tuple where the string is parted into three parts

rsplit()

Splits the string at the specified separator, and returns a list

rstrip()

Returns a right trim version of the string

split()

Splits the string at the specified separator, and returns a list

splitlines()

Splits the string at line breaks and returns a list

startswith()

Returns true if the string starts with the specified value

strip()

Returns a trimmed version of the string

swapcase()

Swaps cases, lower case becomes upper case and vice versa

title()

Converts the first character of each word to upper case

translate()

Returns a translated string

upper()

Converts a string into upper case

zfill()

Fills the string with a specified number of 0 values at the beginning



Escape sequences

In Python, an escape sequence is a combination of characters that represents a special character. Escape sequences are used to represent characters that are not easily entered into a string, such as newline, tab, or quotes.

Here, are some examples of commonly used escape sequences

  1. \n: represents a newline character. It is used to insert a new line in a string.
 string = "Hello\nWorld"
print(string)

//Output:
Hello
World
  1. \t: represents a tab character. It is used to insert a tab space in a string.
 string = "Hello\tWorld"
print(string)

//Output:
Hello World
  1. \": represents a double quote character. It is used to include a double quote within a string that is enclosed in double quotes.
 string = "He said, \"Hello World\""
print(string)

//Output:
He said, "Hello World"

4. '\\': represents a backslash. It is used to include a backslash within a string.

 string = "Hello\\World"
print(string)

//Output:
Hello\World

5.: represents a Carriage return. It is used to move the cursor to the beginning of the current line.
string = "Hello\rWorld"
print(string)

//Output:
Worldllo

These are just a few examples of escape sequences in Python. There are other escape sequences available as well, such as '\a','\b','\f','\v', etc. Each escape sequence has a specific use case, and it's good to familiarize yourself with them to be able to use the appropriate one for a specific task.

Share This:
Facebook Twitter Pinterest Linkedin Whatsapp Whatsapp
python
at January 29, 2023
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Tags python

No comments:

Post a Comment

Newer Post Older Post Home
Subscribe to: Post Comments (Atom)

Popular

Facebook

Subscribe Us

Tags

Android-Hacking Code HTML Programming Language python
Powered by Blogger.

Exclusive content in your inbox

Do you want to be creative? Subscribe to Our Newsletter!
* We promise that we don't spam !

* We promise that we don't spam !

Follow us By Email

Get new post updates by email
Powered by follow.it

FOLLOW US BY EMAIL

Get new post updates by email
Powered by follow.it

Report Abuse

Comptt

Search This Blog

  • March 20236
  • February 202315
  • January 202313

Social Plugin

  • facebook
  • whatsapp
  • instagram
  • youtube
  • twitter
  • linkedin
  • email
  • github
Educational Collaborator

Educational Collaborator

Zeal Education Society Narhe, Pune
Educational Collaborator

Educational Collaborator

JSPM Narhe Technical Campus, Narhe, Pune
Project Manager

Project Manager

Mr. Ayushman Sanjay Dalvi | Project Manager for Technicals
Special Collaborator

Special Collaborator

Cyber Security Mumbai | Special Collaborator for Cyber Security and Contents
  • Home
  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Courses
  • Join Us
  • Affiliate
  • Home-icon
  • Courses
  • Programming Lanuages
  • _HTML
  • _JavaScript
  • _Python
  • _Java
  • Programming Notes
  • Compilers
  • Ethical Hacking
  • About Site
  • _About US
  • _Privacy Policy
  • _Cookie Policy
  • _Terms & Conditions
  • _Contact US
  • Join Us
  • Certificate

Menu Footer Widget

  • Home
  • About
  • Privacy Policy
  • Terms & Conditions
  • Contact Us
  • Instagram
  • Join Us
  • Affiliate

Social Plugin

Our honorable CEO and Experts

Founder and CEO and All the Expert Team Members

Courses Provided by Us

You'll discover all of the most up-to-date bring innovative here.

Special Topics

  • Programming Language

Founder and CEO

Mr. Rohan Kumar Bhoi
Hello, this is Rohan Kumar Bhoi, CEO at CompTT. "Develop more skills to be self-dependent! Learn Rapidly, Be Unique, Live Stylish" facebook twitter youtube instagram linkedin github
    Pune, Maharashtra, India

    Latest posts

    10/recent/ticker-posts
    Project Manager

    Project Manager

    Mr. Aayan Sattar Mulla | Project Manager for Formating and Editing
    Honorable Founder and CEO

    Honorable Founder and CEO

    Mr. Rohan Kumar Bhoi

    "Learn new Skills with Us, and Be SELF DEPENDENT"

    With our easy-to-follow tips and tricks, you'll be self-sufficient in no time! Plus, we have solutions to all your computer-related pro…
    "Learn new Skills with Us, and Be SELF DEPENDENT"

    Courses

    • HTML
    Crafted with by Computer Tips-Triks || Distributed by Mr.Rohan Kumar Bhoi || © Copyright 2019-2023 Computer Tips-Triks All Right Reseved
    Design by - Free Blogger Templates | Distributed by Blogger Templates
    Comptt

    Made for WORLD by

    computertipstricks.tech and designed by Mr. Rohan Kumar Bhoi and Team. It is a one of the best Educational Hub for everyone looking for Computer Knowledge.
    • Home
    • Compilers
    • About Us
    • Privacy Policy
    • Courses
    • Join Us
    • Blog
    • Study Material
    • Home
    • About
    • Contact us
    • Privacy Policy
    • Cookie Policy
    • T & C

    Recent in Posts

    3/Ethical%20Hacking/post-list

    Facebook

    Telegram

    Telegram Group
    Comptt

    Popular Posts

    January 25, 2023

    Pages

    • Contact
    • About
    Created By Themes | Distributed By Gooyaabi Template