FIFA-2022 Career Guide Free Tutorials Go to Your University Placement Preparation 
0 like 0 dislike
3.2k views
in Python Programming by Goeduhub's Expert (3.1k points)
String in Python
by (103 points)
today i learn about strings used in python
by (100 points)
Declaring strings and using strings in python is as simple as using and declaring another variables in any programming language.

6 Answers

1 like 0 dislike
by Goeduhub's Expert (3.1k points)
edited by
 
Best answer

What is String

  1. String is type of  immutable data type of python.
  2. String is defined either in double code "Goeduhub" or in single 'Goeduhub'.
  3. Creating strings is as simple as assigning a value to a variable.

Let's see it by an example 

#Double code

string="Goeduhub Technologies" 

#Single Code

string1='Goeduhub Technologies '

Accessing string elements (String Slicing)

#Defining string 

str = 'Goeduhub Technologies 12345'

print('str = ', str)

#first character

print('str[0] = ', str[0])

#last character

print('str[-1] = ', str[-1])

#slicing 1nd to 5th character

print('str[1:5] = ', str[1:5])

#slicing 5th to 2nd last character

print('str[5:-2] = ', str[5:-2])

#space cosider as string element 

print('str[8]=',str[8])

Output

str =  Goeduhub Technologies 12345
str[0] =  G
str[-1] =  5
str[1:5] =  oedu
str[5:-2] =  hub Technologies 123
str[8]=  

Note: As you can see from the above output string is a heterogeneous type of data type.

Indexing of a String 

Indexing of string is similar to list

Note: From left to right is started from 0 to n, and from right to left it started from -1 to -n.

n- here n is last element of the string

Basic Operation on String

str1 = 'Python'

str2 ='String'

# concatenating strings

print('str1 + str2 = ', str1 + str2)

# printing string multiple time

print('str1 * 2 =', str1 * 2)

Output

str1 + str2 =  PythonString 

str1 * 2 = PythonPython

DELETING AND UPDATING A STRING

Str1 = "Python Programming"

print(Str1) 

# Updating a element of string  

Str1[2] = 'a'

print(Str1) 

 Output

Note: As we know string is immutable means we can't update/ delete a element of string. If we try it, it will give us a error as shown in above output.So, conclusion is if you want to delete an element of a string you have  to delete entire string.

So, basically creating a new string is the solution of this problem, see the example below 

Str1 = "Python Programming"

# Deleting a element of string  

del Str1

# Creating new string

Str1= "Creating new string"

print(Str1)

Output

Creating new string

String Formatting

# Default order 

Str1 = "{} {} ".format('python', 'programming') 

print(Str1) 

# Positional Formatting 

Str2= "{1} {0} ".format('python','programming') 

print(Str2) 

# Keyword Formatting 

Str1 = "{g} {t} {j}".format(g = 'Goeduhub', t= 'Technologies', j = 'Jaipur') 

print(Str1) 

Output

python programming
programming python
Goeduhub Technologies Jaipur

String Function and Methods 

Python has number of functions and methods but here you can see some important function and methods, see the table below

Function/ MethodDescription
string.lowercaseA string must contain lowercase letters.
string.uppercaseA string must contain uppercase letters.
String.indexReturns the position of the first occurrence of substring in a string
replace()returns a copy of the string where all occurrences of a substring is replaced with another substring.
String.IsalnumReturns true if all the characters in a given string are alphanumeric.
String.lenReturns the length of the string.
String.MaxReturns the highest alphabetical character in a string.
string.capitalizeReturn a word with its first character capitalized.
string.splitReturn a list of the words of the string,If the optional second argument sep is absent or None
string.strip()It return a copy of the string with both leading and trailing characters removed

We can also access string elements using loop 

count = 0

for letter in 'Goeduhubuu':

    if(letter == 'u'):

        count += 1

print(count,' times u letter found in given string')

Output

4 times u letter found in given string 

How to use function/ Method in string

str1= "Goeduhub Python programming"

print(len(str1),"Length of given string")

Output

27 Length of given string 


Python Tutorial 

Machine Learning Tutorial 

AI Tutorial

by (101 points)
good material
by (294 points)
this is very good content and i learn fetching the character from index value and also learn  creating new string and string functions and methods
by (422 points)
Everything is clearly mentioned related to string.
by (202 points)
mam i have doubt in string.max,string.split,string.split
can u elaborate these topics or provide examples
by (294 points)
In the python string chapter I  learned that how to declare string and use them
by (100 points)
In this article, i learned about strings in python. The concept of immutable and mutable. Some functions on strings like concatenation, capitalisation, upper, lower, isalnum etc.
by (100 points)
How string.strip() function works??
by (100 points)
We use three double quotes or three single quotes for commenting and also for multiline string, so how python understand is that string or a comment??
0 like 0 dislike
by (134 points)

how we can divide a string in substring in given n part
#like: goeduhub divide it into equal 4 parts

text="goeduhub"

print(text.split)

print([text[i:i+2] for i in range(0,len(text),2)])

output : ['go', 'ed', 'uh', 'ub']

0 like 0 dislike
by (134 points)
i learned about find the memory address and i learned what is immutable or mutable.and also learned the mutable or immutable data types and also learned which are mutable or which are immutable and how can print string in python and how can print each element in python.and i just learned about these functions like extend,append,pop,slicing and how can concatenate in python.
0 like 0 dislike
by (391 points)
Date: 22/04/2020

Topic: Python String

Today i have learn that string VALUE cannot be change, how we can access the string elements, how index formatted and been used, learned some of the function to get our preferable outcomes
0 like 0 dislike
by (182 points)
Module:- python string      Date:-23/04/2020

In this module i have learn about what is string, string slicing.indexing of a string  and basic operation of string(deleting and updating a string,string formating).And i learnt from this module that how to we can access string elements using loop and how to use function/method in string.

Thank you !
by (262 points)
Module:- python string      Date:-23/04/2020
 name : himalay rajwani

In this module i have learn about what is string,how slicing occur how can we access the elements of string with the help of the indexing ,how can we updating and deletion in a string ,with the help of for loop and while loop how can we access the string element ,list slicing and indexing from left to right 0 to n but it also contain negative indexing from right to left -1 to -n  ,etc
0 like 0 dislike
by (232 points)

                                                                                               Topic -python string 

Today i learn about string in python and what is string, string slicing.indexing of a string  and basic operation of string(deleting and updating a string,string formating).And i learnt from this module that how to we can access string elements using loop and how to use function/method in string.

Learn & Improve In-Demand Data Skills Online in this Summer With  These High Quality Courses[Recommended by GOEDUHUB]:-

Best Data Science Online Courses[Lists] on:-

Claim your 10 Days FREE Trial for Pluralsight.

Best Data Science Courses on Datacamp
Best Data Science Courses on Coursera
Best Data Science Courses on Udemy
Best Data Science Courses on Pluralsight
Best Data Science Courses & Microdegrees on Udacity
Best Artificial Intelligence[AI] Courses on Coursera
Best Machine Learning[ML] Courses on Coursera
Best Python Programming Courses on Coursera
Best Artificial Intelligence[AI] Courses on Udemy
Best Python Programming Courses on Udemy

Related questions

0 like 0 dislike
1 answer 391 views
asked Sep 18, 2020 in Python Programming by Nisha Goeduhub's Expert (3.1k points)
0 like 0 dislike
1 answer 2.0k views
1 like 0 dislike
3 answers 2.8k views
0 like 0 dislike
3 answers 2.8k views
0 like 0 dislike
8 answers 6.9k views

 Important Lists:

Important Lists, Exams & Cutoffs Exams after Graduation PSUs

 Goeduhub:

About Us | Contact Us || Terms & Conditions | Privacy Policy ||  Youtube Channel || Telegram Channel © goeduhub.com Social::   |  | 

 

Free Online Directory

...