Ayush Kumar
Ayush Kumar

Reputation: 21

what is difference betwen string and tuple?

Can anyone explain what is difference between string and tuple

a="string"
b=("s","t","r","i","n","g")

both are immutable.

Upvotes: 1

Views: 19107

Answers (6)

Taha Ahmad
Taha Ahmad

Reputation: 1

String are immutable and tuples are also immutable but it's member objects may be mutable

Upvotes: 0

Zahid Hasan
Zahid Hasan

Reputation: 1

t1 = (1,2,3,4)
t2 =(1,2,3,4)
print( t1 is t2)

Output: True

This means they refer to the same object and string does the same thing. But tuples comes into play when few data need to stay together. For example: file name, it's size and type. Even when you return multiple values they are returned as a tuple.

def convert_seconds(seconds):
 hours = seconds//3600
 minutes = (seconds - hours*3600)//60
 remaining_seconds = seconds- hours*3600 - minutes*60
 return hours,minutes,remaining_seconds 
result = convert_seconds(5000)
print(type(result))

output: <class 'tuple'>

Once you know why using it, it will clear your confusion.

Upvotes: 0

Ramineni Ravi Teja
Ramineni Ravi Teja

Reputation: 3906

Strings are immutable in python which means it cannot be changed once created, if you want to update it then a new string is to be created for example.

s="Abcdef"
c=s+'112'
print s,c

you can extract value using index, find values but cannot modify it

To access substrings, use the square brackets for slicing along with the index or indices to obtain your substring

Tuple they are immutable like strings and sequence like lists.They are used to store data just like list, just like string you cannot update or edit the tuple to change it you have to create a new one just like strings.Tuples can be created using parenthesis () and data is inserted using comas.

t1=(1,2,3,'hi')
print type(t1)
print t1  

enter image description here

Upvotes: 1

luoying
luoying

Reputation: 1

tuple use a trailing comma:

tuple_a = 'a',
print(type(tuple_a)) # <class 'tuple'>

string don't use:

string_a = 'a'
print(type(string_a)) # <class 'str'>

but string and tuple has some same characteristics。 eg: 1、indexing and slicing

string_same = 'string'
tuple_same = ('s', 't', 'r', 'i', 'n', 'g')
print(string_same[0], tuple_same[0]) # s s
print(string_same[:-1], tuple_same[:-1]) # strin ('s', 't', 'r', 'i', 'n')

2、Immutability means string and tuple not suport item assigment

string_same[0] = 'python_'
tuple_same[0] = 'python_'
TypeError: 'str' object does not support item assignment
TypeError: 'tuple' object does not support item assignment

you could find all of the diffrent from the Doc. including other tyeps build-in types. https://docs.python.org/3/library/stdtypes.html?highlight=tuple#tuple

Upvotes: -1

Jodast
Jodast

Reputation: 1299

A string is a sequence of unicode characters with quotation marks on either side of the sequence.

Example: mystring = "this is a string"

A tuple is an ordered sequence of objects or characters separated by commas with parentheses on either side of the sequence.

Example: mytuple = (7, "u", "p", 1, "e')

They are, however, similar in the fact that they are both immutable

Upvotes: 0

Adam Smith
Adam Smith

Reputation: 54213

They're different types.

"str" in a  # True
("s", "t", "r") in b  # False

Which means they have different methods, different use cases, different meanings, different implementations, etc. etc. etc.... Consider:

datapoint_tuple = (datetime.datetime.now(), 42)
datapoint_str = ...???

Essentially the only thing they have in common is their immutability.

Upvotes: 2

Related Questions