در پایتون، یک رشته به تعدادی کاراکتر گفته میشود که داخل یک جفت کوتیشن یا دابل کوتیشن قرار بگیرند. اما استفاده از تک کوتیشن بسیار مرسومتر است(که ما در متلب باید از تک کوتیشن استفاده کنیم). نوع داده رشته در پایتون تغییر ناپذیر است یعنی نمیتوانید آنها را تغییر دهید، اگر بخواهید یک رشته را تغییر دهید باید یک رشته جدید بسازید. پایتون یک کلاس داخلی برای نوع داده رشته دارد که به آن str گفته میشود.
.
clc; clear;
% Create a string
s = py.str('this is a string')
s = s.replace('string', 'sentence')
length = py.len(s)
% Convert string to uppercase
s_upper = s.upper()
A = py.str('The quick brown fox jumps over the lazy dog while the sun sets over the horizon, casting long shadows that dance on the meadow.')
% Use Python's textwrap module to wrap the string 'A'
% The 'width' argument is set to 2, meaning each line will contain 2 characters
wrap=py.textwrap.wrap(A,pyargs('width',int8(2)))
% Count the number of occurrences of 'the'
count_the = A.count('the')
% Find the position of 'sun'
pos_sun = A.find('sun')
دیدگاهها
هیچ نظری هنوز ثبت نشده است.